Codeforces Round #291 Div2 C

Problem

N 个字符串 Si ,进行 M 次询问,每次给定一个字符串 S ,问是否存在一个 Si 使得 S Si 长度相等,同时有且只有一个字符不同。

Limits

TimeLimit(ms):3000

MemoryLimit(MB):256

N,M[0,3×105]

6×105

Look up Original Problem From here

Solution

用字典树可解,但用hash会很方便。

双hash,seed=1e4级别的素数,mod1=1e9+7,mod2=1e9+9

Complexity

TimeComplexity:O(|S|all×log2N)

MemoryComplexity:O(|S|max+N)

My Code

//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef unsigned long long ull;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define MAXN 1001000
#define N
#define M
ll idx(char c){
    return c-'a'+1;
}
ll seed1[MAXN],seed2[MAXN];
const ll seed=49681;
const ll mod1=1e9+7;
const ll mod2=1e9+9;
int n,m,lens;
map<pair<ll,ll>,bool>here;
char s[MAXN];
int main(){
    seed1[0]=seed2[0]=1;
    rep(i,1,MAXN){
        seed1[i]=seed1[i-1]*seed%mod1;
        seed2[i]=seed2[i-1]*seed%mod2;
    }
    scanf("%d %d",&n,&m);
    repin(num,1,n){
        scanf("%s",s);
        lens=len(s);
        ll v1=0,v2=0;
        rep(i,0,lens){
            v1=(v1*seed+idx(s[i]))%mod1;
            v2=(v2*seed+idx(s[i]))%mod2;
        }
        pair<ll,ll>p=make_pair(v1,v2);
        here[p]=true;
    }
    repin(num,1,m){
        scanf("%s",s);
        lens=len(s);
        ll v1=0,v2=0;
        rep(i,0,lens){
            v1=(v1*seed+idx(s[i]))%mod1;
            v2=(v2*seed+idx(s[i]))%mod2;
        }
        ll v1t,v2t;
        bool ok=false;
        rep(i,0,lens){
            if(ok) break;
            for(char c='a';c<='c';c++){
                if(s[i]==c) continue;
                v1t=v1+(idx(c)-idx(s[i]))*seed1[lens-1-i];
                v1t=(v1t%mod1+mod1)%mod1;
                v2t=v2+(idx(c)-idx(s[i]))*seed2[lens-1-i];
                v2t=(v2t%mod2+mod2)%mod2;
                pair<ll,ll>p=make_pair(v1t,v2t);
                if(here.find(p)!=here.end()){
                    ok=true;
                    break;
                }
            }
        }
        printf("%s\n",ok?"YES":"NO");
    }
}

你可能感兴趣的:(字符串,双hash)