对应SPOJ题目:点击打开链接
Time Limit: 159MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Given a string, we need to find the total number of its distinct substrings.
Input
T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 50000
Output
For each test case output one number saying the number of distinct substrings.
Example
Sample Input:
2
CCCCC
ABABA
Sample Output:
5
9
Explanation for the testcase with string ABABA:
len=1 : A,B
len=2 : AB,BA
len=3 : ABA,BAB
len=4 : ABAB,BABA
len=5 : ABABA
Thus, total number of distinct substrings is 9.
Source
题意:求给出的每个字符串的不相同子串数目
思路:后缀数组(不相同子串个数)的基本应用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MS(x, y) memset(x, y, sizeof(x))
const int MAXN = 1000+10;
const int INF = 1<<30;
int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN];
int rank[MAXN],r[MAXN],sa[MAXN],height[MAXN];
char str[MAXN];
int 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 *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for(i=0; i<m; i++) ws[i] = 0;
for(i=0; i<n; i++) ws[x[i] = r[i]]++;
for(i=1; i<m; i++) ws[i] += ws[i-1];
for(i=n-1; i>=0; i--) sa[--ws[x[i]]] = i;
for(j=1,p=1; p<n; j<<=1, 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++) wv[i] = x[y[i]];
for(i=0; i<m; i++) ws[i] = 0;
for(i=0; i<n; i++) ws[wv[i]]++;
for(i=1; i<m; i++) ws[i] += ws[i-1];
for(i=n-1; i>=0; i--) sa[--ws[wv[i]]] = y[i];
for(t=x,x=y,y=t,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++;
}
return;
}
void calheight(int *r, int *sa, int n)
{
int i, j, k = 0;
for(i=1; i<n; i++) rank[sa[i]] = i;
for(i=0; i<n-1; height[rank[i++]] = k)
for(k ? k-- : 0,j=sa[rank[i]-1]; r[i+k] == r[j+k]; k++);
return;
}
int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
MS(r, 0);
MS(sa, 0);
MS(rank, 0);
MS(height, 0);
MS(ws, 0);
MS(wv, 0);
MS(wa, 0);
MS(wb, 0);
scanf("%s", str);
int i;
int len = strlen(str);
for(i=0; i<len; i++)
r[i] = (int)str[i] - 31;//可显示字符的范围是 32~126,
r[len++] = 0;
da(r, sa, len, 100);
calheight(r, sa, len);
int ans = 0;
for(i=1; i<len; i++)
ans += len - 1 - sa[i] - height[i];
printf("%d\n", ans);
}
return 0;
}