#include using namespace std; #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() #define spa << " " << #define lfs #define test cout<<"test"<= (ll)(n); i--) using ll = int; using ld = long double; const ll MOD = 1e9+7; //const ll MOD = 998244353; //const ll INF = 1e18; using P = pair; template void chmin(T &a,T b){if(a>b)a=b;} template void chmax(T &a,T b){if(a void ans(bool x,T1 y,T2 z){if(x)cout< void debug(vector>&v,ll h,ll w){for(ll i=0;i&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){if(n!=0)cout< vector>vec(ll x, ll y, T w){ vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vectordx={1,0,-1,0,1,1,-1,-1}; vectordy={0,1,0,-1,1,-1,1,-1}; template vector make_v(size_t a,T b){return vector(a,b);} template auto make_v(size_t a,Ts... ts){ return vector(a,make_v(ts...)); } struct SuffixArray{ ll size; string str; vectordata; SuffixArray(string S):size(S.size()),str(S){ str += '$'; vectorinput(str.size()); for(ll i=0;i induced_sort(vectort, ll kind){ ll sz = t.size(); vectorls(sz);//trueはL型,falseはS型 for(ll i = sz-1;i>=0;i--){ if(i==sz-1)ls[i] = false; else{ if(t[i]!=t[i+1])ls[i] = (t[i] > t[i+1]); else ls[i] = ls[i+1]; } } vectorcnt(kind); for(ll i=0;ilr(kind,MP(-1,-1)); for(ll i=1;ilmsidx,ret(sz,-1); for(ll i=0;i=1&&ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].fi+cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; if(i!=0&&!ls[ret[i]])ret[i]=-1; } } fill(ALL(cnt),0); for(ll i=sz-1;i>=1;i--){ if(ret[i]>=1&&!ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].se-cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; } } vectorrev_lmsidx(sz,-1),lmsinput(lmsidx.size(),-1); for(ll i=0;icomp(t.begin()+ret[0],t.end()); for(ll i=1;i=1&&ls[ret[i]-1]&&!ls[ret[i]]){ vectortmp(t.begin()+ret[i],t.begin()+lmsidx[rev_lmsidx[ret[i]]+1]+1); if(comp != tmp){ kindnum++; comp = tmp; } lmsinput[rev_lmsidx[ret[i]]] = kindnum; } } vectoroutput; if(kindnum==lmsidx.size()){ output.assign(kindnum,-1); for(ll i=0;i=0;i--){ ll tmp=lmsidx[output[i]]; ret[lr[t[tmp]].se - cnt[t[tmp]]]=tmp; cnt[t[tmp]]++; } fill(ALL(cnt),0); for(ll i=0;i=1&&ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].fi+cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; if(i!=0&&!ls[ret[i]])ret[i]=-1; } } fill(ALL(cnt),0); for(ll i=sz-1;i>=1;i--){ if(ret[i]>=1&&!ls[ret[i]-1]){ ret[lr[t[ret[i]-1]].se-cnt[t[ret[i]-1]]]=ret[i]-1; cnt[t[ret[i]-1]]++; } } return ret; } ll operator[](ll x) const { return data[x]; } ll lower_bound(string x){ ll le=-1,ri=str.size(); while(ri-le>=2){ ll mid=(le+ri)/2; ll cnt=0; while(cnt>s; SuffixArray sa(s); ll m;cin>>m; while(m--){ string c;cin>>c; res+=sa.upper_bound(c)-sa.lower_bound(c); } cout<