SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)

SUBST1 - New Distinct Substrings

no tags 

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

Input:
2
CCCCC
ABABA

Output:
5
9


ac代码

#include     
#include     
#include     
#include    
#define min(a,b) (a>b?b:a) 
#define max(a,b) (a>b?a:b)  
using namespace std;    
char str[50010];  
int sa[50100],Rank[50100],rank2[50100],height[50010],c[50100],*x,*y,s[50100];
int n;
void cmp(int n,int sz)
{
	int i;
	memset(c,0,sizeof(c));
	for(i=0;i=0;i--)
		sa[--c[x[y[i]]]]=y[i];
}
void build_sa(int *s,int n,int sz)
{
	x=Rank,y=rank2;
	int i,j;
	for(i=0;i=len)
				y[yid++]=sa[i]-len;
			cmp(n,sz);
		swap(x,y);
		x[sa[0]]=yid=0;
		for(i=1;i=n)
			break;
	}
	for(i=0;i


你可能感兴趣的:(后缀数组)