题意:给你n个字符串,m次查询,每次问你第p个字符串的s到t的字符串在n个字符串建成的字典树上出现了多少次
题解:先建出字典树,在字典树上拓展sam,记录每个子串的出现次数.查询时只需找出在字典树上的t在sam中的位置,每次往fa跳(即后缀相同,长度减小)找到第一个长度比查询串的小于等于的位置就是答案.往fa上跳的过程,能用倍增优化.(预处理倍增数组写错,wa了很久....)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=1000000+10,maxn=1000000+10,inf=0x3f3f3f3f;
struct SAM{
int last,cnt;
int ch[N<<1][26],fa[N<<1],l[N<<1],sz[N<<1];
int a[N<<1],c[N<<1],father[N<<1][21];
int ins(int y,int x)
{
last=y;
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
if(!p)fa[np]=1;
else
{
int q=ch[p][x];
if(l[q]==l[p]+1)fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
sz[np]=1;
return last;
}
void build()
{
cnt=last=1;
}
void topo()
{
for(int i=1;i<=cnt;i++)c[l[i]]++;
for(int i=1;i<=cnt;i++)c[i]+=c[i-1];
for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i;
}
void gao()
{
topo();
for(int i=cnt;i>=1;i--)
{
int p=a[i];
sz[fa[p]]+=sz[p];
father[i][0]=fa[i];
}
for(int i=1;i<=20;i++)for(int j=1;j<=cnt;j++)
father[j][i]=father[father[j][i-1]][i-1];
}
}sam;
char s[N];
vector<int>v[100010];
struct Tire{
int tot,ch[N][26],id[N],root;
int newnode()
{
memset(ch[tot],0,sizeof ch[tot]);
return ++tot;
}
Tire()
{
tot=0;
root=newnode();
}
void ins(int id)
{
int now=root,len=strlen(s+1);
v[id].resize(len+2);
for(int i=1;i<=len;i++)
{
if(!ch[now][s[i]-'a'])ch[now][s[i]-'a']=newnode();
now=ch[now][s[i]-'a'];
v[id][i]=now;
}
}
void bfs()
{
queue<int>q;q.push(root);id[root]=1;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=0;i<26;i++)if(ch[u][i])
{
id[ch[u][i]]=sam.ins(id[u],i);
q.push(ch[u][i]);
}
}
}
}t;
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
t.ins(i);
}
sam.build();
t.bfs();
sam.gao();
int m;scanf("%d",&m);
while(m--)
{
int p,x,y;scanf("%d%d%d",&p,&x,&y);
p=t.id[v[p][y]];x=y-x+1;
for(int i=20;i>=0;i--)if(sam.l[sam.father[p][i]]>=x)p=sam.father[p][i];
printf("%d
",sam.sz[p]);
}
return 0;
}
/********************
********************/