当前位置:   article > 正文

POJ3461 字符串哈希_poj3461()hash

poj3461()hash
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<set>
  8. #include<map>
  9. #include<queue>
  10. #include<vector>
  11. #include<stack>
  12. using namespace std;
  13. #define ll long long
  14. #define ull unsigned long long
  15. #define mxn 1002000
  16. #define eps 1e-8
  17. char t[mxn], w[mxn];
  18. ull h[mxn], p[mxn];
  19. ull geth( int l, int r ) {
  20. return h[r] - h[ l - 1 ] * p[ r - l + 1];
  21. }
  22. int main() {
  23. int cas;
  24. p[0] = 1;
  25. for( int i = 1; i < mxn; ++i )
  26. p[i] = p[i-1] * 133;
  27. scanf( "%d", &cas );
  28. while( cas-- ) {
  29. scanf( "%s", w + 1 );
  30. scanf( "%s", t + 1 );
  31. ull cnt = 0;
  32. int wn = strlen( w + 1 );
  33. for( int i = 1; i <= wn; ++i )
  34. cnt = cnt * 133 + w[i];
  35. h[0] = 0;
  36. int n = strlen( t + 1 );
  37. for( int i = 1; i <= n; ++i )
  38. h[i] = h[i-1] * 133 + t[i];
  39. int ans = 0;
  40. for( int i = 1; i <= n - wn + 1; ++i )
  41. if( cnt == geth( i, i + wn - 1 ) )
  42. ans++;
  43. printf( "%d\n", ans );
  44. }
  45. return 0;
  46. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/998756
推荐阅读
相关标签
  

闽ICP备14008679号