E. Mike and Friends
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!
In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n.
Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).
Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF.
The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number
Input
The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105).
The next n lines contain the phone numbers, i-th line contains a string si consisting of lowercase English letters ().
The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).
Output
Print the answer for each question in a separate line.
Examples
input
5 5
a
ab
abab
ababab
b
1 5 1
3 5 1
1 5 2
1 5 3
1 4 5
output
7
5
6
3
6
题意:给出 (n) 个字符串, (q) 个询问 (l,r,k) ,询问 ([l,r]) 的字符串中包含多少个 (k) 串作为字串。
题解:这题看了别人的博客,感觉做法非常暴力,没想到线段树可以这么用。
以下是粘贴的部分:
将字符串拼接起来,求一次后缀数组,求后缀数组时保存每个字符属于哪个串,每个串的起始位置,然后我们以 (sa) 数组作为下标建立线段树,线段树每个节点代表的区间,保存这个区间内每个字符属于哪个串,然后排序
对于一个询问包含的串 (k),找到 (k) 的起点在 (sa) 上的位置,我们可以二分其在 (sa) 数组上能延伸的最左端点以及最右端点,使得这之间的所有串与 (k) 的 (lcp) 都等于 (k) 的串长。然后我们只要在线段树内统计编号大于等于 (l) 且小于等于 (r) 的数个数就好了,这个我们只要在每个被完全包含的区间内二分答案,然后累加即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=2e5+10;
/*
*suffix array
*倍增算法 O(n*logn)
*待排序数组长度为n,放在0~n-1中,在最后面补一个0
*build_sa( ,n+1,m+1); //注意是n+1,m是s数组中的最大值;
*getHeight(,n);
*例如:
*n = 8;
*num[] = { 1, 1, 2, 1, 1, 1, 1, 2, $ };注意num最后一位为0,其他大于0
*rank[] = { 4, 6, 8, 1, 2, 3, 5, 7, 0 };rank[0~n-1]为有效值,rank[n]必定为0无效值
*sa[] = { 8, 3, 4, 5, 0, 6, 1, 7, 2 };sa[1~n]为有效值,sa[0]必定为n是无效值
*height[]= { 0, 0, 3, 2, 3, 1, 2, 0, 1 };height[2~n]为有效值
*
*/
int sa[maxn*2];//SA数组,表示将S的n个后缀从小到大排序后把排好序的
//的后缀的开头位置顺次放入SA中
int t1[maxn*2],t2[maxn*2],c[maxn*2];//求SA数组需要的中间变量,不需要赋值
int rk[maxn*2],height[maxn*2];
//待排序的字符串放在s数组中,从s[0]到s[n-1],长度为n,且最大值小于m,
//除s[n-1]外的所有s[i]都大于0,r[n-1]=0
//函数结束以后结果放在sa数组中
void build_sa(int s[],int n,int m)
{
int i,j,p,*x=t1,*y=t2;
//第一轮基数排序,如果s的最大值很大,可改为快速排序
for(i=0;i<m;i++)c[i]=0;
for(i=0;i<n;i++)c[x[i]=s[i]]++;
for(i=1;i<m;i++)c[i]+=c[i-1];
for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
for(j=1;j<=n;j<<=1)
{
p=0;
//直接利用sa数组排序第二关键字
for(i=n-j;i<n;i++)y[p++]=i;//后面的j个数第二关键字为空的最小
for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
//这样数组y保存的就是按照第二关键字排序的结果
//基数排序第一关键字
for(i=0;i<m;i++)c[i]=0;
for(i=0;i<n;i++)c[x[y[i]]]++;
for(i=1;i<m;i++)c[i]+=c[i-1];
for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
//根据sa和x数组计算新的x数组
swap(x,y);
p=1;x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n)break;
m=p;//下次基数排序的最大值
}
}
void getHeight(int s[],int n)
{
int i,j,k=0;
for(i=0;i<=n;i++) rk[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k)k--;
j=sa[rk[i]-1];
while(s[i+k]==s[j+k])k++;
height[rk[i]]=k;
}
}
int cnt;
char s[maxn];
int a[maxn*2],st[maxn],sz[maxn];
int bl[maxn*2];
VI seg[maxn*8];
void build(int i,int l,int r)
{
seg[i].clear();
rep(j,l,r+1) seg[i].pb(bl[sa[j]]);
sort(seg[i].begin(),seg[i].end());
if(l==r) return;
int m=(l+r)/2;
build(i*2,l,m);
build(i*2+1,m+1,r);
}
int query(int i,int l,int r,int L,int R,int x,int y)
{
if(l==L&&r==R)
{
int t1=lower_bound(seg[i].begin(),seg[i].end(),x)-seg[i].begin()-1;
int t2=lower_bound(seg[i].begin(),seg[i].end(),y+1)-seg[i].begin()-1;
return t2-t1;
}
int m=(L+R)/2;
if(r<=m) return query(i*2,l,r,L,m,x,y);
else if(l>m) return query(i*2+1,l,r,m+1,R,x,y);
else return query(i*2,l,m,L,m,x,y)+query(i*2+1,m+1,r,m+1,R,x,y);
}
int d[maxn*2][19];
void init_RMQ(int n)
{
for(int i=1;i<=n;i++) d[i][0]=height[i];
for(int k=1;(1<<k)<=n;k++)
for(int i=0;i+(1<<k)<=n;i++)
d[i][k]=min(d[i][k-1],d[i+(1<<(k-1))][k-1]);
}
int query(int l,int r)
{
int k=0;
while(1<<(k+1)<=r-l+1) k++;
return min(d[l][k],d[r-(1<<k)+1][k]);
}
int lcp(int l,int r)
{
if(l==r) return cnt-sa[l];
if(l>r) swap(l,r);
if(l+1==r) return height[r];
else return query(l+1,r);
}
int getr(int x,int k)
{
int l=x,r=cnt;
int ans=-1;
while(l<=r)
{
int m=(l+r)/2;
if(lcp(m,x)>=k)
{
ans=m;
l=m+1;
}
else r=m-1;
}
return ans;
}
int getl(int x,int k)
{
int l=1,r=x;
int ans=-1;
while(l<=r)
{
int m=(l+r)/2;
if(lcp(m,x)>=k)
{
ans=m;
r=m-1;
}
else l=m+1;
}
return ans;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
int mx='z'-'a'+2;
cnt=0;
rep(i,1,n+1)
{
scanf("%s",s);
int l=(int)strlen(s);
sz[i]=l;
st[i]=cnt;
rep(j,0,l)
{
a[cnt]=s[j]-'a'+1;
bl[cnt]=i;
cnt++;
}
if(i<n)
a[cnt]=mx++,bl[cnt++]=n+1;
}
a[cnt]=0,bl[cnt]=n+1;
build_sa(a,cnt+1,mx+2);
getHeight(a,cnt);
build(1,1,cnt);
init_RMQ(cnt);
while(q--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int t=sz[k];
k=rk[st[k]];
int fl=getl(k,t),fr=getr(k,t);
int ans=query(1,fl,fr,1,cnt,l,r);
printf("%d
",ans);
}
return 0;
}