codeforces 514C Watto and Mechanism

字符串哈希,枚举每一位即可

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#define DEBUG(x) cout<<#x<<" = "<<x<<endl
using namespace std;
typedef long long ll;
const int MAXN=6e5+10;
const ll MOD=1e15+37;
const ll p=1009;
set<ll> mem;
ll my_pow[MAXN];
ll get_hash(char s[],int len)
{
    ll r=0;
    for(int i=0;i<len;i++ ){
        r=(r*p+(s[i]-'a'+1)+MOD)%MOD;
    }
    return r;
}
void get_pow()
{
    my_pow[0]=1;
    ///最左边是最高位
    for(int i=1;i<MAXN ;i++ ){
        my_pow[i]=(my_pow[i-1]*p)%MOD;
    }
}
char s[MAXN];
int main()
{
    freopen("in.txt","r",stdin);
    int n,m;
    get_pow();
    scanf("%d%d",&n,&m);
    while(n--){
        scanf("%s",s);
        int len=strlen(s);
        ll t=get_hash(s,len);
        mem.insert(t);
    }
    while(m--){
        scanf("%s",s);
        int len=strlen(s);
        bool ok=false;
        ll h=get_hash(s,len);
        for(int i=0;i<len ;i++ ){
            for(int j=0;j<3 ;j++ ){
                char c='a'+j;
                if(s[i]==c)continue;
                ll t=(h+my_pow[len-i-1]*(c-s[i])+3*MOD)%MOD;
                if(mem.count(t)){
                    ok=true;break;
                }
            }
            if(ok)break;
        }
        ok?printf("YES\n"):printf("NO\n");
    }
}

 

转载于:https://www.cnblogs.com/MalcolmMeng/p/9801965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值