当前位置:   article > 正文

Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings(Hash)_educational codeforces round 42 (rated for div. 2)

educational codeforces round 42 (rated for div. 2) d.merge equals

题目链接:http://codeforces.com/contest/985/problem/F


按照字符转为01串,直接Hash就好,然后判断的时候,直接排序判断,就可以消除字符之间的区别。


代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int MAXN=2e5+5;
  5. const int MOD=1e9+7;
  6. int po[MAXN],f[MAXN][26];
  7. int a[26],b[26];
  8. char s[MAXN];
  9. bool judge(int x,int y,int len)
  10. {
  11. for(int j=0;j<26;j++)
  12. {
  13. a[j]=(f[x+len-1][j]-1LL*f[x-1][j]*po[len]%MOD+MOD)%MOD;
  14. b[j]=(f[y+len-1][j]-1LL*f[y-1][j]*po[len]%MOD+MOD)%MOD;
  15. }
  16. sort(a,a+26);sort(b,b+26);
  17. for(int j=0;j<26;j++)
  18. if(a[j]!=b[j]) return false;
  19. return true;
  20. }
  21. int main()
  22. {
  23. //freopen("in.txt","r",stdin);
  24. //freopen("out.txt","w",stdout);
  25. int n,m;
  26. scanf("%d%d",&n,&m);
  27. scanf("%s",s+1);
  28. po[0]=1;
  29. for(int i=1;i<=n;i++)
  30. po[i]=po[i-1]*2%MOD;
  31. for(int i=1;i<=n;i++)
  32. {
  33. for(int j=0;j<26;j++)
  34. {
  35. f[i][j]=(f[i-1][j]*2+((s[i]-'a')==j?1:0)%MOD)%MOD;
  36. }
  37. }
  38. while(m--)
  39. {
  40. int x,y,len;
  41. scanf("%d%d%d",&x,&y,&len);
  42. if(judge(x,y,len)) puts("YES");
  43. else puts("NO");
  44. }
  45. return 0;
  46. }

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

闽ICP备14008679号