POJ 3461 Oulipo(字符串hash)

 

题目链接

 

字符串hash判断字符串是否相等。

 

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4  
 5 using namespace std;
 6  
 7 typedef unsigned long long uLL;
 8 const uLL B = 131;
 9  
10 uLL f[10010];
11 uLL Hash[1000100];
12 char a[10010],b[1000100];
13  
14  
15 int main () {
16     int T;
17     scanf("%d",&T);
18     f[0] = 1;
19     for (int i=1; i<=10000; ++i) f[i] = (f[i-1] * B);
20     while (T--) {
21         int ans = 0;
22         scanf("%s%s",a,b);
23         int s1 = strlen(a),s2 = strlen(b);
24         uLL Ha = 0;
25         for (int i=0; i<s1; ++i) {
26             Ha = (Ha*B+a[i]);
27         }
28         Hash[0] = b[0];
29         for (int i=1; i<s2; ++i) {
30             Hash[i] = Hash[i-1]*B+b[i];
31         }
32         for (int i=0; i<=(s2-s1); ++i) {
33             int l = i,r = l+s1-1;
34             uLL Hb = Hash[r]-Hash[l-1]*f[s1];
35             if (Ha == Hb) ans++;
36         }
37         printf("%d\n",ans);
38     }
39     return 0;
40 }

 

posted @ 2018-03-23 07:32  MJT12044  阅读(146)  评论(0编辑  收藏  举报