CoderForces985F-Isomorphic Strings
题意:在一个字符串里随意找两个长度相同的子串,比较是否为同构字符串;
字符串hash,比较两个字符串的每个字符的hash值是否相同;
AC;
#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;
}
字符串