• 题意
    • 给出一个串,在其中任取两段长度相同的子串,问是否同构
    • 同构在本题中的意思是两个串的结构相同,比如 ”hffcf“ 与 ”pooco“同为ABBCBC型串
  • 题解
    • 直接将字符串的每个区间每个字母的哈希值处理出来
    • 在询问时将每个得到的哈希值排序一一比较即可
  • 代码
    • #include<iostream>
      #include<queue>
      #include<cstdio>
      #include<vector>
      #include<algorithm>
      using namespace std;
      typedef long long ll;
      const int maxn=2e5+10;
      const ll MOD=1e9+7;
      ll h[26][maxn],x=107,px[maxn];
      char s[maxn];
      int main()
      {
          int n,m;
          scanf("%d%d", &n,&m);
          scanf("%s", s+1);
          px[0]=1;
          for(int i = 1; i <= n; ++i) px[i]=px[i-1]*x%MOD;
          for(int i = 0; i < 26; ++i)
      	{
              for(int j = 1; j <= n; ++j) h[i][j]=(h[i][j-1]*x+int(s[j] == (i+'a')))%MOD;
          }
          while(m--)
      	{
              int x,y,l;
              scanf("%d%d%d", &x,&y,&l);
              vector<int> p,q;
              for(int i = 0; i < 26; ++i)
      		{
                  p.push_back(((h[i][x+l-1]-px[l]*h[i][x-1]%MOD)%MOD+MOD)%MOD);
                  q.push_back(((h[i][y+l-1]-px[l]*h[i][y-1]%MOD)%MOD+MOD)%MOD);
              }
              sort(q.begin(),q.end());sort(p.begin(),p.end());
              printf("%s\n", p==q? "YES":"NO");
          }
          return 0;
      }

发表评论

邮箱地址不会被公开。 必填项已用*标注