团队里的hash水题,数据小的不用hash都能过。。
也就是前缀hash,后缀hash,再比较一下就行。
——代码
1 #include <cstdio> 2 #include <cstring> 3 #define ULL unsigned long long 4 5 int n, m, ans; 6 char s1[81], s2[81]; 7 ULL base[81], sum1[81], sum2[81], bit1[81], bit2[81]; 8 9 inline int max(int x, int y) 10 { 11 return x > y ? x : y; 12 } 13 14 inline int min(int x, int y) 15 { 16 return x < y ? x : y; 17 } 18 19 int main() 20 { 21 int i; 22 s1[0] = s2[0] = '0'; 23 scanf("%s %s", s1 + 1, s2 + 1); 24 n = strlen(s1); 25 m = strlen(s2); 26 base[0] = 1; 27 for(i = 1; i < max(n, m); i++) base[i] = base[i - 1] * 107; 28 for(i = 1; i < n; i++) sum1[i] = sum1[i - 1] * 107 + s1[i]; 29 for(i = 1; i < m; i++) sum2[i] = sum2[i - 1] * 107 + s2[i]; 30 for(i = n - 1; i >= 1; i--) bit1[i] = bit1[i + 1] + s1[i] * base[n - 1 - i]; 31 for(i = m - 1; i >= 1; i--) bit2[i] = bit2[i + 1] + s2[i] * base[m - 1 - i]; 32 for(i = 1; i < min(n, m); i++) 33 if(sum1[i] == bit2[m - i] || sum2[i] == bit1[n - i]) 34 ans = i; 35 printf("%d ", ans); 36 return 0; 37 }