题目:
http://lightoj.com/volume_showproblem.php?problem=1314
题意:
求母串中长度在p到q之间的不相同子串的个数。
思路:
求不相同子串个数和明显用后缀数组可以搞,对于每个后缀能够取得子串个数就是min(q,n-sa[i])-max(p,height[i]+1)+1 很好理解就是能够取得的最大长度减去重复或者是最小长度,因为要去掉相同部分所以是取height[i]+1。
代码:
//kopyh
#include <bits/stdc++.h>
#include <stdio.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define N 500100
using namespace std;
int n,m,sum,res,flag;
char s[N];
int seq[N], sa[N], ranks[N], height[N];
int wwa[N], wwb[N], wws[N], wwv[N];
bool cmp(int r[], int a, int b, int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
}
void da(int r[],int n, int m)
{
int i, j, p, *x = wwa, *y = wwb;
for (i = 0; i < m; ++i) wws[i] = 0;
for (i = 0; i < n; ++i) wws[x[i]=r[i]]++;
for (i = 1; i < m; ++i) wws[i] += wws[i-1];
for (i = n-1; i >= 0; --i) sa[--wws[x[i]]] = i;
for (j = 1, p = 1; p < n; j *= 2, m = p)
{
for (p = 0, i = n - j; i < n; ++i) y[p++] = i;
for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = 0; i < n; ++i) wwv[i] = x[y[i]];
for (i = 0; i < m; ++i) wws[i] = 0;
for (i = 0; i < n; ++i) wws[wwv[i]]++;
for (i = 1; i < m; ++i) wws[i] += wws[i-1];
for (i = n-1; i >= 0; --i) sa[--wws[wwv[i]]] = y[i];
for (swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)
x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;
}
}
void calheight(int r[], int n)
{
int i, j, k = 0;
for (i = 1; i <= n; ++i) ranks[sa[i]] = i;
for (i = 0; i < n; height[ranks[i++]] = k)
for (k?k--:0, j = sa[ranks[i]-1]; r[i+k] == r[j+k]; k++);
}
int main()
{
int i,j,k,cas,T,t,x,y,p,q,z;
scanf("%d",&T);
cas=0;
while(T--)
{
scanf("%s",s);
scanf("%d%d",&p,&q);
n=strlen(s);m=27;
for(i=0;i<n;i++)
seq[i]=s[i]-'a'+1;
seq[n]=0;
da(seq,n+1,m);
calheight(seq,n);
res=0;
for(i=1;i<=n;i++)
{
x=max(p,height[i]);
y=min(q,n-sa[i]);
if(y-x+1>0)res+=y-x+1;
}
printf("Case %d: %d\n",++cas,res);
}
return 0;
}