#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1000005,M=2000005,K=26,E=20000005;
int n,m,q,ql,qr;
char s[N],t[N];
int lim[N];
namespace sams
{
int ind,la,tot;
int dir[N][K],len[N],fail[N],fro[N],fr[N],nxt[N],em[N],lc[E],rc[E],now[E],sye[E],poe[N];
inline void addedge(int u,int v)
{
static int tot=0;
++tot;
nxt[tot]=fr[u];
fr[u]=tot;
em[tot]=v;
}
#define mid ((l+r)>>1)
void ins(int &p,int l,int r,int q,int x)
{
if(p==0)
{
p=++tot;
lc[p]=rc[p]=now[p]=0;
sye[p]=x;
}
if(l==r)
{
now[p]++;
return;
}
if(q<=mid)
ins(lc[p],l,mid,q,x);
else
ins(rc[p],mid+1,r,q,x);
now[p]=now[lc[p]]+now[rc[p]];
}
int query(int p,int l,int r,int ql,int qr)
{
if(p==0)
return 0;
if(ql<=l&&r<=qr)
return now[p];
int ans=0;
if(l<=qr&&ql<=mid)
ans+=query(lc[p],l,mid,ql,qr);
if(mid+1<=qr&&ql<=r)
ans+=query(rc[p],mid+1,r,ql,qr);
return ans;
}
inline int merge(int p,int q,int l,int r,int x)
{
if(p==0)
return q;
if(q==0)
return p;
if(sye[p]==x)
now[p]+=now[q];
else
{
++tot;
lc[tot]=lc[p];
rc[tot]=rc[p];
now[tot]=now[p];
sye[tot]=x;
p=tot;
now[p]+=now[q];
}
if(l==r)
return p;
lc[p]=merge(lc[p],lc[q],l,mid,x);
rc[p]=merge(rc[p],rc[q],mid+1,r,x);
now[p]=now[lc[p]]+now[rc[p]];
return p;
}
#undef mid
void ins(int c,int le)
{
++ind;
ins(fro[ind],1,n,le,ind);
poe[ind]=le;
len[ind]=len[la]+1;
while(la!=0&&dir[la][c]==0)
{
dir[la][c]=ind;
la=fail[la];
}
if(la==0&&dir[la][c]==0)
{
dir[la][c]=ind;
fail[ind]=la;
la=ind;
return;
}
else if(len[dir[la][c]]==len[la]+1)
{
fail[ind]=dir[la][c];
la=ind;
return;
}
else
{
int p=dir[la][c];
++ind;
len[ind]=len[la]+1;
fail[ind]=fail[p];
poe[ind]=poe[p];
ins(fro[ind],1,n,poe[ind],ind);
memmove(dir[ind],dir[p],sizeof(dir[ind]));
fail[p]=ind;
fail[ind-1]=ind;
for(int j=la;dir[j][c]==p;j=fail[j])
dir[j][c]=ind;
la=ind-1;
return;
}
}
void build(int d)
{
for(int j=fr[d];j!=0;j=nxt[j])
{
build(em[j]);
fro[d]=merge(fro[d],fro[em[j]],1,n,d);
}
}
};
namespace samt
{
int ind,la;
int dir[M][K],len[M],fail[M],pos[M];
inline void clear()
{
ind=la=0;
memset(dir[0],0,sizeof(dir[0]));
}
void ins(int c,int le)
{
++ind;
memset(dir[ind],0,sizeof(dir[ind]));
pos[ind]=le;
len[ind]=len[la]+1;
while(la!=0&&dir[la][c]==0)
{
dir[la][c]=ind;
la=fail[la];
}
if(la==0&&dir[la][c]==0)
{
dir[la][c]=ind;
fail[ind]=la;
la=ind;
return;
}
else if(len[dir[la][c]]==len[la]+1)
{
fail[ind]=dir[la][c];
la=ind;
return;
}
else
{
int p=dir[la][c];
++ind;
len[ind]=len[la]+1;
fail[ind]=fail[p];
pos[ind]=pos[p];
memmove(dir[ind],dir[p],sizeof(dir[ind]));
fail[p]=ind;
fail[ind-1]=ind;
for(int j=la;dir[j][c]==p;j=fail[j])
dir[j][c]=ind;
la=ind-1;
return;
}
}
};
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
sams::ins(s[i]-'a',i);
for(int i=1;i<=sams::ind;i++)
sams::addedge(sams::fail[i],i);
sams::build(0);
scanf("%d",&q);
while(q--)
{
scanf("%s",t+1);
m=strlen(t+1);
scanf("%d%d",&ql,&qr);
samt::clear();
for(int i=1;i<=m;i++)
samt::ins(t[i]-'a',i);
for(int i=1,now=0,le=0;i<=m;i++)
{
while(1)
{
if(sams::dir[now][t[i]-'a']!=0&&sams::query(sams::fro[sams::dir[now][t[i]-'a']],1,n,ql+le,qr)>0)
{
now=sams::dir[now][t[i]-'a'];
le++;
break;
}
if(le==0)
break;
le--;
if(le==sams::len[sams::fail[now]])
now=sams::fail[now];
}
lim[i]=le;
}
ll ans=0;
for(int i=1;i<=samt::ind;i++)
ans+=max(0,samt::len[i]-max(samt::len[samt::fail[i]],lim[samt::pos[i]]));
printf("%lld\n",ans);
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 2.65 ms | 212 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 3.585 ms | 660 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 3.758 ms | 664 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 40.791 ms | 1 MB + 88 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 39.383 ms | 1 MB + 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 870.702 ms | 481 MB + 504 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 871 ms | 480 MB + 720 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 140.452 ms | 75 MB + 332 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 141.562 ms | 60 MB + 440 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 303.3 ms | 145 MB + 408 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 332.297 ms | 124 MB + 324 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 492.413 ms | 216 MB + 996 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 550.471 ms | 192 MB + 968 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 683.858 ms | 293 MB + 84 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 784.255 ms | 265 MB + 68 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 892.599 ms | 369 MB + 352 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 1.025 s | 338 MB + 188 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 528.304 ms | 153 MB + 308 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 697.246 ms | 223 MB + 24 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 873.553 ms | 295 MB + 288 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 1.023 s | 369 MB + 852 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 1.054 s | 369 MB + 184 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 1.05 s | 369 MB + 120 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 1.039 s | 369 MB + 728 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 1.033 s | 369 MB + 892 KB | Accepted | Score: 4 | 显示更多 |