Educational Codeforces Round 44 F. Isomorphic Strings

题目链接:F. Isomorphic Strings

题意:先给个字符串s,然后m次询问,每次询问给个x,y,len,问分别以x,y开始长度为len的子串是否同构,同构的意思就是一个字母只映射到另一个字母,然后原串,和其映射的串,就互为同构。

题解:hash.把26个字符的位置hash值前缀求出来,每次询问把26个字母的hash值求出排下序,判断看26个hash值是否一样,

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
int bas=3;
const int maxn=2e5+5;
const int mod=1e9+7;
char s[maxn];
ll has[26][maxn];
ll a[26],b[26],p[maxn];
bool judge(int x,int y,int len)
{
    for(int i=0;i<26;i++)
    {
        a[i]=((has[i][x+len-1]-(has[i][x-1]*p[len])%mod)+mod)%mod;
        b[i]=((has[i][y+len-1]-(has[i][y-1]*p[len]))%mod+mod)%mod;
    }
    sort(a,a+26);sort(b,b+26);
    for(int i=0;i<26;i++)
    {
        if(a[i]!=b[i])return false;
    }
    return true;
}
int main()
{
    scanf("%d %d",&n,&m);
    scanf("%s",s+1);
    p[0]=1;
    for(int i=1;i<=n;i++)
    {
        p[i]=(p[i-1]*bas)%mod;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<26;j++)
        {
            has[j][i]=(has[j][i-1]*bas+((s[i]-'a')==j))%mod;
        }
    }
    while(m--)
    {
        int x,y,len;
        scanf("%d %d %d",&x,&y,&len);
        if(judge(x,y,len))printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/lhclqslove/p/9088969.html