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<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> 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<class T>
pair<T,T> operator+(const pair<T,T> &l,const pair<T,T>& r){
    return {(l.f+r.f)%MOD,(l.s+r.s)%MOD};
}

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

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

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

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

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

struct hsh{
    string S;
    vector<pl> po,ipo;
    vector< array<pl,26> >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<pl,26> 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");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值