hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
生活随笔
收集整理的這篇文章主要介紹了
hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
題目傳送門
1 /* 2 hash+set:首先把各個字符串的哈希值保存在set容器里,然后對于查詢的每一個字符串的每一位進行枚舉 3 用set的find函數查找是否存在替換后的字符串,理解后并不難。另外,我想用64位的自然溢出wa了,不清楚 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-5 13:05:49 8 * File Name :D.cpp 9 ************************************************/ 10 11 #include <cstdio> 12 #include <algorithm> 13 #include <iostream> 14 #include <sstream> 15 #include <cstring> 16 #include <cmath> 17 #include <string> 18 #include <vector> 19 #include <queue> 20 #include <deque> 21 #include <stack> 22 #include <list> 23 #include <map> 24 #include <set> 25 #include <bitset> 26 #include <cstdlib> 27 #include <ctime> 28 using namespace std; 29 30 #define lson l, mid, rt << 1 31 #define rson mid + 1, r, rt << 1 | 1 32 typedef long long ll; 33 const int MAXN = 6e5 + 10; 34 const int INF = 0x3f3f3f3f; 35 const int MOD = 1e9 + 7; 36 const int KEY = 257; 37 char s[MAXN]; 38 set<ll> S; 39 ll ha[MAXN]; 40 int n, m; 41 42 void init(void) { 43 ha[0] = 1; 44 for (int i=1; i<MAXN; ++i) ha[i] = ha[i-1] * KEY % MOD; 45 } 46 47 ll get_hash(char *s) { 48 int len = strlen (s); 49 ll res = 0; 50 for (int i=0; i<len; ++i) { 51 res = (res * KEY + s[i]) % MOD; 52 } 53 return res; 54 } 55 56 bool judge(char *s) { 57 int len = strlen (s); 58 ll h = get_hash (s); 59 for (int i=0; i<len; ++i) { 60 for (ll ch='a'; ch<='c'; ++ch) { 61 if (ch == s[i]) continue; 62 if (S.find ((((ch-s[i]) * ha[len-i-1] + h) % MOD + MOD) % MOD) != S.end ()) return true; 63 } 64 } 65 return false; 66 } 67 68 int main(void) { //Codeforces Round #291 (Div. 2) C. Watto and Mechanism 69 init (); 70 while (scanf ("%d%d", &n, &m) == 2) { 71 S.clear (); 72 for (int i=1; i<=n; ++i) { 73 scanf ("%s", s); 74 S.insert (get_hash (s)); 75 } 76 for (int i=1; i<=m; ++i) { 77 scanf ("%s", s); 78 if (judge (s)) puts ("YES"); 79 else puts ("NO"); 80 } 81 } 82 83 return 0; 84 }?
轉載于:https://www.cnblogs.com/Running-Time/p/4705101.html
總結
以上是生活随笔為你收集整理的hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Activiti之 Exclusive
- 下一篇: struts2使用注解--ACTION中