【CodeForces - 514C】Watto and Mechanism(字符串哈希)
生活随笔
收集整理的這篇文章主要介紹了
【CodeForces - 514C】Watto and Mechanism(字符串哈希)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題干:
輸入n個字符串,然后進(jìn)行m次詢問,每次詢問輸入一個字符串,問n個字符串中是否存在與當(dāng)前輸入的字符串長度相等,并且剛好有且僅有一個位置的字符不同。存在則輸出YES,不存在輸出NO。
Examples
Input
2 3 aaaaa acacaca aabaa ccacacc caaacOutput
YES NO NO解題報告:
? ?剛開始讀錯題了,還以為需要對于每一個詢問,看n個模板串的子串是否有滿足要求的解。但是題目中還要求兩個串的長度需要相同,這樣就簡單多了,直接對于每個模板串,存下Hash值,直接放入set,對于每個詢問,暴力修改位置,然后在set中查詢就行了,剛開始單模數(shù)WA17了,臉黑、、改雙模數(shù)就過了。(其實(shí)雙模數(shù)用pair是最好的,因?yàn)檫@樣有坑出現(xiàn):兩個Hash分別沖突了不同的值,也就是還是又概率出現(xiàn)沖突的,但是直接set<pair<ull,ull> >就幾乎完全不會有問題)(wjhnb)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; typedef unsigned ll ull; const int MAX = 6e5 + 5; const ll mod = 2000004199,mod2 = 1111111111111; const ll seed = 131,seed2 = 13331; int n,m; char s[MAX]; ull P[MAX],P2[MAX]; set<ull> ss,ss2; int main() {P[0]=P2[0]=1;for(int i = 1; i<MAX; i++) P[i] = P[i-1] * seed % mod,P2[i] = P2[i-1] * seed2 % mod2;cin>>n>>m;for(int i = 1; i<=n; i++) {scanf("%s",s+1);ull Hash = 0,Hash2 = 0;int len = strlen(s+1);for(int j = 1; j<=len; j++) Hash = (Hash*seed + s[j]-'a'+1)%mod,Hash2 = (Hash2*seed2+s[j]-'a'+1)%mod2;ss.insert(Hash);ss2.insert(Hash2);}for(int i = 1; i<=m; i++) {scanf("%s",s+1);ull Hash=0,Hash2 = 0;int len = strlen(s+1),flag = 0;for(int j = 1; j<=len; j++) Hash = (Hash*seed + s[j]-'a'+1)%mod,Hash2 = (Hash2*seed2+s[j]-'a'+1)%mod2;for(char ch = 'a'; ch<='c'; ch++) {for(int j = 1; j<=len; j++) {if(s[j] == ch) continue;if(ss.count((Hash+10*mod + (ch - s[j]) * P[len-j])%mod) && ss2.count((Hash2+10*mod2+(ch-s[j])*P2[len-j])%mod2)) {flag = 1;break; }}}if(flag == 1) puts("YES");else puts("NO");}return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 514C】Watto and Mechanism(字符串哈希)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GDP排名前十的一大国,因货币贬值要被挤
- 下一篇: 饿了么免单答案上热搜 网友化身福尔摩斯