contest985F. Isomorphic Strings+二重哈希+幂次方构造

F. Isomorphic Strings
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s of length n consisting of lowercase English letters.

For two given strings s and t, say S is the set of distinct characters of s and T is the set of distinct characters of t. The strings s and t are isomorphic if their lengths are equal and there is a one-to-one mapping (bijection) f between S and T for which f(si) = ti. Formally:

  1. f(si) = ti for any index i,
  2. for any character there is exactly one character that f(x) = y,
  3. for any character there is exactly one character that f(x) = y.

For example, the strings "aababc" and "bbcbcz" are isomorphic. Also the strings "aaaww" and "wwwaa" are isomorphic. The following pairs of strings are not isomorphic: "aab" and "bbb", "test" and "best".

You have to handle m queries characterized by three integers x, y, len (1 ≤ x, y ≤ n - len + 1). For each query check if two substrings s[x... x + len - 1] and s[y... y + len - 1] are isomorphic.

Input

The first line contains two space-separated integers n and m (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105) — the length of the string s and the number of queries.

The second line contains string s consisting of n lowercase English letters.

The following m lines contain a single query on each line: xi, yi and leni (1 ≤ xi, yi ≤ n, 1 ≤ leni ≤ n - max(xi, yi) + 1) — the description of the pair of the substrings to check.

Output

For each query in a separate line print "YES" if substrings s[xi... xi + leni - 1] and s[yi... yi + leni - 1] are isomorphic and "NO" otherwise.

Example
Input
Copy
7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3
Output
Copy
YES
YES
NO
YES
Note

The queries in the example are following:

  1. substrings "a" and "a" are isomorphic: f(a) = a;
  2. substrings "ab" and "ca" are isomorphic: f(a) = c, f(b) = a;
  3. substrings "bac" and "aba" are not isomorphic since f(b) and f(c) must be equal to a at same time;
  4. substrings "bac" and "cab" are isomorphic: f(b) = c, f(a) = a, f(c) = b.


#define happy

#include
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair pi;
typedef pair pl;
typedef pair pd;

typedef vector vi;
typedef vector vd;
typedef vector vl;
typedef vector vpi;
typedef vector vpl;


#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second



const ll INF=1e18;
const int MOD=1e9+7;

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

template
pair operator+(const pair &l,const pair& r){
    return {(l.f+r.f)%MOD,(l.s+r.s)%MOD};
}

template
pair operator-(const pair &l,const pair& r){
    return {(l.f-r.f+MOD)%MOD,(l.s-r.s+MOD)%MOD};
}

template
pair operator*(const pair &l,const T& r){
    return {(l.f*r)%MOD,(l.s*r)%MOD};
}

template
pair operator*(const pair &l,const pair& r){
    return {(l.f*r.f)%MOD,(l.s*r.s)%MOD};
}

array operator-(const array&a,const array&b){
    array c;rep(i,0,25)c[i]=a[i]-b[i];
    return c;
}

array operator*(const pl &a,const array&b){
    array c;rep(i,0,25)c[i]=b[i]*a;
    return c;
}

struct hsh{
    string S;
    vector po,ipo;
    vector< array >cum;
    pl base,invbase;

    ll modpow(ll b,ll p){
        return !p?1:modpow(b*b%MOD,p/2)*(p&1?b:1)%MOD;
    }

    ll inv(ll x){
        return modpow(x,MOD-2);
    }

    void gen(string _S){
        S=_S;
        po.resize(sz(S)),ipo.resize(sz(S)),cum.resize(sz(S)+1);
        po[0]=ipo[0]={1,1};
        base.s=rand()%MOD;
        base.f=rand()%MOD;
        invbase.s=inv(base.s);
        invbase.f=inv(base.f);
        rep(i,1,sz(S)-1){
            po[i]=po[i-1]*base;
            ipo[i]=ipo[i-1]*invbase;
        }
        rep(i,0,sz(S)-1){
            cum[i+1]=cum[i];
            cum[i+1][S[i]-'a']=cum[i+1][S[i]-'a']+po[i];
        }
    }

    vpl get(int l,int r){
        array a=ipo[l]*(cum[r+1]-cum[l]);
        vpl z;rep(i,0,25)z.pb(a[i]);
        sort(all(z));
        return z;
    }
};

hsh H;

int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    int n=rd(),m=rd();
    string t;
    cin>>t;
    H.gen(t);
    rep(i,0,m-1){
        int x=rd()-1,y=rd()-1,len=rd();
        if(H.get(x,x+len-1)==H.get(y,y+len-1))
            puts("YES");
        else puts("NO");
    }
}

你可能感兴趣的:(contest985F. Isomorphic Strings+二重哈希+幂次方构造)