跳到正文

剪贴板

剪贴板 76p1q5nz

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>

using namespace std;
using ll = long long;
#define mhas(x, y) (1ll * (x) * mod[1] + (y))

const int N = 1e5 + 5, M = 1e6 + 5;
const int mod[2] = {998244353, 100000007}, bas[2] = {301, 13331};

int n, q, top;
ll pos;

struct node{
    ll p;
    int cnt;
}mp[M];
string s, t;

struct Hsh{
    vector<int> b[2], qp[2];
    inline void init(const string &s){
        for(register int k = 0; k < 2; k++) b[k].clear(), qp[k].clear();
        for(register int i = 0; i <= s.size(); i++) for(register int k = 0; k < 2; k++) b[k].push_back(0), qp[k].push_back(1);
        for(register int i = 1; i <= s.size(); i++) for(register int k = 0; k < 2; k++) b[k][i] = (1ll * b[k][i - 1] * bas[k] + s[i - 1]) % mod[k];
        for(register int i = 1; i <= s.size(); i++) for(register int k = 0; k < 2; k++) qp[k][i] = 1ll * qp[k][i - 1] * bas[k] % mod[k];
    }
    inline ll query(int l, int r){
        int res1 = (b[0][r] - 1ll * qp[0][r - l + 1] * b[0][l - 1] % mod[0] + mod[0]) % mod[0];
        int res2 = (b[1][r] - 1ll * qp[1][r - l + 1] * b[1][l - 1] % mod[1] + mod[1]) % mod[1];
        return mhas(res1, res2);
    }
}has, dhs;

struct Tire{
    int tot, cnt[M], pre[M], g[M][26];
    inline void Insert(const string &s, int id){
        int rt = 0;
        for(register int i = 0; i < s.size(); i++){
            if(!g[rt][s[i] - 97]) g[rt][s[i] - 97] = ++tot;
            rt = g[rt][s[i] - 97], cnt[rt]++;
            mp[++top] = {mhas(dhs.b[0][i + 1], dhs.b[1][i + 1]), rt};
        }
    }
    inline void solve(int rt){
        pre[rt] += cnt[rt];
        for(register int j = 0; j < 26; j++){
            if(!g[rt][j]) continue;
            pre[g[rt][j]] = pre[rt], solve(g[rt][j]);
        }
    }
}tr;

bool cmp(const node &ad, const node &bd){
    return ad.p < bd.p;
}

namespace fastio
{
    #ifdef __linux__
    #define gc getchar_unlocked
    #define pc putchar_unlocked
    #else
    #define gc _getchar_nolock
    #define pc _putchar_nolock
    #endif
    inline bool blank(const char x){return!(x^32)||!(x^10)||!(x^13)||!(x^9);}template<typename Tp>inline void read(Tp&x){x=0;register bool z=true;register char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=(x<<1)+(x<<3)+(a^48);x=(z?x:~x+1);}inline void read(double&x){x=0.0;register bool z=true;register double y=0.1;register char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}inline void read(char&x){for(x=gc();blank(x)&&(x^-1);x=gc());}inline void read(char*x){register char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())*x++=a;*x=0;}inline void read(string&x){x="";register char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}template<typename T,typename...Tp>inline void read(T&x,Tp&...y){read(x),read(y...);}template<typename Tp>inline void write(Tp x){if(!x)return pc(48),void();if(x<0)pc('-'),x=~x+1;register int len=0;register char tmp[64];for(;x;x/=10)tmp[++len]=x%10+48;while(len)pc(tmp[len--]);}inline void write(const double x){register int a=6;register double b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;register double y=5*powl(10,-a-1);b+=y,c+=y;register int len=0;register char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}inline void write(const pair<int,double>x){register int a=x.first;if(a<7){register double b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;register double y=5*powl(10,-a-1);b+=y,c+=y;register int len=0;register char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else cout<<fixed<<setprecision(a)<<x.second;}inline void write(const char x){pc(x);}inline void write(const bool x){pc(x?49:48);}inline void write(char*x){fputs(x,stdout);}inline void write(const char*x){fputs(x,stdout);}inline void write(const string&x){fputs(x.c_str(),stdout);}template<typename T,typename...Tp>inline void write(T x,Tp...y){write(x),write(y...);}
}
using namespace fastio;

int main(){
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    read(s, n);
    has.init(s);
    for( int i = 1; i <= n; i++){
        read(t);
        dhs.init(t), tr.Insert(t, i);
    }
    tr.solve(0);
    read(q);
    sort(mp + 1, mp + top + 1, cmp);
    for(register int i = 1, ld, rd; i <= q; i++){
        read(ld, rd);
        int l = ld, r = rd, mid = 0, ans = -1, lf = 1, rf = top, midd = 0, sum = 0;
        while(l <= r){
            mid = (l + r) / 2;
            lf = 1, rf = top, midd = 0, sum = 0;
            pos = has.query(ld, mid);
            while(lf <= rf){
                midd = (lf + rf) / 2;
                if(mp[midd].p <= pos) lf = midd + 1, sum = midd;
                else rf = midd - 1;
            }
            if(mp[sum].p == pos) l = mid + 1, ans = mp[sum].cnt;
            else r = mid - 1;
        }
        if(ans == -1){
            cout << "0\n";
            continue;
        }
        write(tr.pre[ans]), putchar('\n');
    }
    return 0;
}