| ||||||||||
ReincarnationTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2012 Accepted Submission(s): 683
Problem Description
Now you are back,and have a task to do:
Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s. And you have some query,each time you should calculate f(s[l...r]), s[l...r] means the sub-string of s start from l end at r.
Input
The first line contains integer T(1<=T<=5), denote the number of the test cases.
For each test cases,the first line contains a string s(1 <= length of s <= 2000). Denote the length of s by n. The second line contains an integer Q(1 <= Q <= 10000),denote the number of queries. Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n), denote a query.
Output
For each test cases,for each query,print the answer in one line.
Sample Input
Sample Output
|
题意:给一个字符串,给出一个范围,问在这个范围内不同的子串有多少。
思路:这个跟求整个串的不同字串有点像。可以先把这个范围内的每个位置的sa保存到pos数组中,然后遍历。遍历的时候维护一个da,表示这个字符串与排在他前面的字符串的最长公共前缀,也就是跟前面重复的部分(就像求整个串的不同字串要减去height[i]一样,这里要减去da),还有维护一个last,表示前面最长公共前缀的位置,如何维护last,详见代码注释。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=10010;
const int INF=1000000000;
int sa[maxn],height[maxn],rank[maxn],t[maxn],t2[maxn],c[maxn];
int n,pos[maxn],d[maxn][20];
char str[maxn];
void build_sa(int m,int n)
{
int *x=t,*y=t2;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[i]=str[i]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=n-k;i<n;i++)y[p++]=i;
for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[y[i]]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
x[sa[0]]=0;p=1;
for(int i=1;i<n;i++)
x[sa[i]]=(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++);
if(p>=n)break;
m=p;
}
}
void getheight(int n)
{
int k=0;
for(int i=1;i<=n;i++)rank[sa[i]]=i;
for(int i=0;i<n;i++)
{
if(k)k--;
int j=sa[rank[i]-1];
while(str[i+k]==str[j+k])k++;
height[rank[i]]=k;
}
}
void initRMQ()
{
for(int i=0;i<=n;i++)d[i][0]=height[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=1;(i+(1<<(j-1)))<=n;i++)
d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int LCP(int a,int b)
{
int x=rank[a],y=rank[b];
if(x>y)swap(x,y);
x++;
int k=0;
while((1<<(k+1))<=(y-x+1))k++;
return min(d[x][k],d[y-(1<<k)+1][k]);
}
void solve()
{
int q;
scanf("%d",&q);
while(q--)
{
int l,r,ans=0;
scanf("%d%d",&l,&r);
for(int i=0;i<=n;i++)pos[i]=-1;
for(int i=l;i<=r;i++)pos[rank[i-1]]=i-1;
int da=INF,last=-1;
for(int i=1;i<=n;i++)
{
if(pos[i]==-1)continue;
if(last!=-1)
{
da=min(LCP(pos[i],last),r-last);
ans+=r-pos[i]-min(da,r-pos[i]);
}
else if(last==-1)ans+=r-pos[i];
//last保存最长公共前缀的位置,如果当前后缀是last的前缀,那么最长的还是last,这个跟求整个串的不同字串部不同
//因为如果是整个串没有范围限制,减去height就减去了所有重复的。这样做是要排除:如果当前位置是后面某个的前缀
//后面的这个串个能跟前面的有更多的重复,那么就加多了。所以要维护last为最长的公共前缀
if(last!=-1&&r-last>r-pos[i])
{
int tmp=LCP(last,pos[i]);
if(tmp>=r-pos[i])last=last;
else last=pos[i];
}
else last=pos[i];
}
printf("%d\n",ans);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
n=strlen(str);
build_sa(123,n+1);
getheight(n);
initRMQ();
solve();
}
return 0;
}