Description
Flybrother is a Superman, therefore he's always busy saving the world.
To graduate from Nudt are boring but necessary for him. Typically We need to post a paper to get graduate Certificate, however being one Superman, Flybrother wants to make his p Aper Perfect. A paper is a lower case string. To make it perfect, flybrother wanna the number of different substrings in the paper. It's quite a silly problem for flybrother, but because he's so busy, can do help him to solve it?
Input
There is several cases. Process till EOF.
For each case, there was a line of lower case string (1<=length<=100000).
Output
For each case, output one number in a line of the answer described in the problem.
Sample Input
Aaab
Sample Output
15
HINT
Test instructions: Ask for a different number of strings
Idea: In my suffix array topic summary has the same topic, template problem
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <bitset># Include <algorithm> #include <climits>using namespace std; #define LS 2*i#define RS 2*i+1#define Up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __gcd (A, b) #define LL long long#define N 100005#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e- 8LL Wa[n],wb[n],wsf[n],wv[n],sa[n]; LL Rank1[n],height[n],s[n],a[n];char Str[n],str1[n],str2[n];//sa: The starting position of the rank I bit in the dictionary sequence is sa[i]//rank in str: That is, the suffix of the str i-position is the longest common prefix in the dictionary ordinal row//height: The suffix of the dictionary sequence I and i-1 ll CMP (ll *r,ll a,ll b,ll k) {return r[a]==r[b]&&r[a+k]==r[b +K];} void Getsa (ll *r,ll *sa,ll n,ll m)//n to include the end added 0{LL i,j,p,*x=wa,*y=wb,*t; for (i=0; i<m; i++) wsf[i]=0; for (i=0; i<n; i++) wsf[x[i]=r[i]]++; for (I=1; i<m; i++) wsf[i]+=wsf[i-1]; for (i=n-1; i>=0; i--) sa[--wsf[x[i]]]=i; P=1; J=1; for (; 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++) wv[i]=x[y[i]; for (i=0; i<m; i++) wsf[i]=0; for (i=0; i<n; i++) wsf[wv[i]]++; for (I=1; i<m; i++) wsf[i]+=wsf[i-1]; for (i=n-1; i>=0; i--) sa[--wsf[wv[i]]]=y[i]; T=x; X=y; y=t; x[sa[0]]=0; for (P=1,i=1; i<n; i++) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p-1:p++; }}void getheight (ll *r,ll N)//n do not save the last 0{LL i,j,k=0; for (I=1; i<=n; i++) rank1[sa[i]]=i; for (i=0; i<n; i++) {if (k) k--; else k=0; J=SA[RANK1[I]-1]; while (R[i+k]==r[j+k]) k++; Height[rank1[i]]=k; }}ll T,ans,n,m;int Main () {LL i,j,k,len; W (~scanf ("%s", str)) {len = strlen(str); Up (i,0,len-1) s[i]=str[i]; S[len] = 0; Getsa (s,sa,len+1,300); GetHeight (S,len); Ans = (1+len) *LEN/2; Up (I,2,len) ans-=height[i]; printf ("%lld\n", ans); }}
Csu1656:paper of flybrother (suffix array)