hash

#include<bits/stdc++.h> 
using namespace std;
#define int long long
#define all(a) (a).begin(), (a).end()
typedef __int128_t i128;
typedef __uint128_t u128;
const int N = 1e5+7;
vector<int> c[200];
vector<int> d[200];
u128 h[26][N];
u128 p[N];
signed main()
{
    freopen("test.in","r",stdin);
    freopen("test.res","w",stdout);

    int n,m;
    string s;
    cin>>n>>m>>s;
    for(int i=0;i<n;i++)
        c[s[i]].push_back(i);
    for(int i='a';i<='z';i++)
    {
        d[i].push_back(-1);
        d[i].push_back(-1);
        int t = c[i].size();
        for(int j=1;j<c[i].size();j++)
            d[i].push_back(c[i][j]-c[i][j-1]);
        h[i-'a'][0] = 1;
        p[0] = 1;
        for(int j=1;j<=t;j++)
        {
            h[i-'a'][j] = h[i-'a'][j]*13331+d[i][j];
            p[i] = p[i-1]*13331;
        }
    }
    auto get = [&] (u128 i,u128 l,u128 r) -> u128
    {
        return h[i-'a'][r]-h[i-'a'][l-1]*p[r-l+1];
    };

    for(int i=0;i<m;i++)
    {
        int l1,r1,l2,r2;
        cin>>l1>>r1>>l2>>r2;
        l1--;
        l2--;
        r1--;
        r2--;
        set<char> ans;
        for(char j='a';j<='z';j++)   
        {
            int k = 0;
            int st0 = lower_bound(all(c[j]),l1) - c[j].begin();
            int st1 = lower_bound(all(c[j]),l2) - c[j].begin();
            if((st0>=c[j].size()||c[j][st0]>r1)&&(st1>=c[j].size()||c[j][st1]>r2))
                continue;
            if((st0>=c[j].size()||c[j][st0]>r1)||(st1>=c[j].size()||c[j][st1]>r2))
            {
                ans.insert(j);
                continue;
            }
            if(c[j][st0]-l1!=c[j][st1]-l2)
            {
                ans.insert(j);
                continue;
            }
            int ed0 = upper_bound(all(c[j]),r1) - c[j].begin();
            int ed1 = upper_bound(all(c[j]),r2) - c[j].begin();
            if(ed0-st0!=ed1-st1)
            {
                ans.insert(j);
                continue;
            }
            if(ed0-st0==1)  continue;
            if(get(j,st0+1,ed0)!=get(j,st1+1,ed1))
            {
                ans.insert(j);
                continue;
            }
        }
        cout<<ans.size()<<"\n";
        for(auto c:ans)
            cout<<c;
        cout<<"\n";
    }

}
posted @ 2024-06-30 17:20  holy_crap  阅读(5)  评论(0编辑  收藏  举报