题目链接:http://codeforces.com/contest/985/problem/F
https://acm.njupt.edu.cn/problem/CF985F
题意:
给出两个相同长度的字符串,其中a串每一个x都有唯一的f(x)=y转化b串,同时b串中每一个y都有唯一的f(y)=x转化为a串
解题思路:
利用Hash,对26个字母在字符串中的位置赋予Hash值
下面我直接引用我看到的一篇题解:
https://blog.csdn.net/hqg_ac/article/details/85246661
也就是给的两个子串中,相同出现顺序性的字母必须成对出现
还有就是Hash的时候,我取p=31 37时WA掉了
取p=17 47 131 147等等值AC了
不是很明白(莫非是巧合?)
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define ll unsigned long long
const int mod=1e9+7;
const int p=37;
ll Hash[220000][27];
ll Pow[220000];
int n,m;
int L,R,len;
char s[220000];
void get_hash()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=26;j++)
{
Hash[i][j]=(Hash[i-1][j]*p+(s[i]==('a'+j-1)))%mod;
}
}
}
int check()
{
ll a[27];
ll b[27];
for(int i=1;i<=26;i++)
{
a[i]=(Hash[L+len-1][i]+mod-Hash[L-1][i]*Pow[len]%mod)%mod;
b[i]=(Hash[R+len-1][i]+mod-Hash[R-1][i]*Pow[len]%mod)%mod;
}
sort(a+1,a+27);
sort(b+1,b+27);
for(int i=1;i<=26;i++)
if(a[i]!=b[i])
return 0;
return 1;
}
int main()
{
cin>>n>>m;
Pow[0]=1;
for(int i=1;i<=n;i++)
Pow[i]=Pow[i-1]*p%mod;
scanf("%s",s+1);
get_hash();
while(m--)
{
scanf("%d%d%d",&L,&R,&len);
if(check())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}