codeforces514c

这是个字符串哈希的问题,先给他字符串哈希一下,然后再做,这个问题还有一个就是,你需要一个字母一个字母改值,然后再进行字符串哈希查找,这才能找到正确的字符串哈希位置。然后更改当前位置的权值,最后看set里面是否有这个哈希值
提题意给字典,然后给测试单词,如果有一个不相同的话也算字典里的单词儿,然后看测试单词儿是否是字典里的单词儿

#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;
const int N = 655360;
const int M = 1e9 + 7;

LL f[N] = {1};
int n , m;
char s[N];
setst;

void init()//处理每个位置的权值
{
    for(int i = 1 ; i < N ; i++){
        f[i] = f[i - 1]*257%M;
    }
}

LL run(char *s)//计算单词儿的哈希值
{
    LL res = 0;
    int l = strlen(s);
    for(int i = 0 ; i < l ; i++){
        res = (res * 257 + s[i])%M;
    }
    return res;
}

int judge(char *s)//判断一发
{
    LL h = run(s);
    int l = strlen(s);
    for(int i = 0 ; i < l ; i++){
        for(LL c = 'a' ; c <= 'c' ; c++){
            if(s[i] == c) continue;
            //if(st.find((((c-s[i])*f[l - i + 1] + h)%M + M) % M) != st.end()) return 1;
              if(st.find((((c-s[i])*f[l-i-1]+h)%M+M)%M)!=st.end()) return 1;//改权值
        }
    }
    return 0;
}
int main()
{
    init();
    scanf("%d %d", &n , &m);
    for(int i = 0 ; i < n ; i++)
        scanf("%s",s),st.insert(run(s));
    for(int i = 0 ; i < m ; i++)
        scanf("%s",s),puts(judge(s)?"YES":"NO");
    return 0;
}

你可能感兴趣的:(哈希)