gpt4 book ai didi

c++ - 我的solve(字符串a,字符串b)的输出与Shuffle Hashing中的预期输出不匹配

转载 作者:行者123 更新时间:2023-12-02 10:22:37 25 4
gpt4 key购买 nike

我们使用由小写字母组成的密码p,并在其中随机地随机排列字母以获得p'(p'仍可以等于p);
生成两个由小写字母s1和s2组成的随机字符串(这些字符串中的任何一个都可以为空);
生成的哈希h = s1 + p'+ s2,其中加法是字符串连接。
我们的输入必须为否。测试用例

对于每个测试用例,一个密码和一个哈希密码(在不同的行中),
每个测试用例的输出必须为"is"或“否”,这取决于是否可以根据给定的密码构造给定的哈希。

#include<iostream>
#include<vector>
#define forn(i,n) for(int i=0;i<n;i++)
using namespace std;

void solve(string p, string h) {
vector<int> pcnt(26);
int ps = p.size();
int hs = h.size();
forn(j, ps) {
++pcnt[p[j] - 'a'];
forn(j, hs) {
vector<int> hcnt(26);
for (int m = j; m < j+ps; m++) {
++hcnt[h[m] - 'a'];
if (pcnt == hcnt) {
puts ("YES");
return;
}
}
}

}

puts("NO");

}

int main() {
int t;
cin >> t;
forn(i, t) {
string p, h;
cin >> p >> h;
solve(p, h);
}
}

用于输入
1
one
zzonneyy

我的输出是
YES

我不知道为什么。请帮帮我吗?
这是关于代码强制问题的 link

最佳答案

您的代码段存在几个问题。

  • forn(j, hs)已被使用两次,因此j的范围难以确定
    理解。
  • 第一个字符与匹配时,
  • (pcnt == hcnt)条件检查将退出
  • 用于混淆代码的宏的用法。 #define forn(i,n) for(int i=0;i<n;i++) Macro's are evil

  • 找到可以解决您问题的以下代码段,
    void solve (string p, string h)
    {
    std::vector <int> pcnt (26);
    int ps = p.size ();
    int hs = h.size ();

    //To create a finger print for given password
    for (int j = 0; j < ps; ++j)
    {
    ++pcnt[p[j] - 'a'];
    }

    vector <int>hcnt (26);

    //Moving frame to check the matching finger print
    for (int i = 0; i < hs; ++i)
    {
    ++hcnt[h[i] - 'a'];
    if (i - ps >= 0){
    --hcnt[h[i - ps] - 'a'];
    }

    if (pcnt == hcnt){
    puts ("YES");
    return;
    }
    }

    puts ("NO");
    }

    关于c++ - 我的solve(字符串a,字符串b)的输出与Shuffle Hashing中的预期输出不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59464959/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com