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
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++)
int rankarr[MAXSIZE],wa[MAXSIZE],wb[MAXSIZE],height[MAXSIZE];
int wvarr[MAXSIZE],wsarr[MAXSIZE],SA[MAXSIZE];
char str[MAXSIZE];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(char *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0; i<m; i++) wsarr[i]=0;
for(i=0; i<n; i++) wsarr[x[i]=r[i]]++;
for(i=1; i<m; i++) wsarr[i]+=wsarr[i-1];
for(i=n-1; i>=0; i--) sa[--wsarr[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++) wvarr[i]=x[y[i]];
for(i=0; i<m; i++) wsarr[i]=0;
for(i=0; i<n; i++) wsarr[wvarr[i]]++;
for(i=1; i<m; i++) wsarr[i]+=wsarr[i-1];
for(i=n-1; i>=0; i--) sa[--wsarr[wvarr[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(char *r,int *sa,int n)
{
int i,j,k=0;
for(i=1; i<=n; i++) rankarr[sa[i]]=i;
for(i=0; i<n; height[rankarr[i++]]=k)
for(k?k--:0,j=sa[rankarr[i]-1]; r[i+k]==r[j+k]; k++);
return;
}
LL solve(int n)
{
LL ans=n;
for(int i=2;i<=n;i++)
ans+=n-i+1-height[i];
return ans;
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
scanf("%s",str);
int n=strlen(str);
str[n]=0;
da(str,SA,n+1,128);
calheight(str,SA,n);
cout<<solve(n)<<endl;
}
return 0;
}