#include <iostream>#include<cstring>#include<Set>#include<map>#include<cmath>#include<stack>#include<queue>#include<deque>#include<list>#include<algorithm>#include<stdio.h>#include<iomanip>#defineRep (i,n) for (int i=0;i<n;++i)#defineFab (i,a,b) for (int i=a;i<=b;++i)#defineFBA (I,b,a) for (int i=b;i>=a;--i)#definePB push_back#defineINF 0x3f3f3f3f#defineMP Make_pair#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineSF scanf#definePF printf#defineLL Long LongConst intn=1005;using namespaceStd;typedef pair<int,int>PII;intWa[n],wb[n],wv[n],wd[n];intSa[n],rank[n],height[n];intcmpint*r,intAintBintl) { returnR[a] = = R[b] && r[a+l] = = r[b+L]; } //r array of length n from [0,n]//the SA obtained from [1,n]//Call time R[n-1]=0voidDaint*r,intNintm) {//multiplication Algorithm R is a range of characters for which the array n to be matched is the total length m intI, J, p, *x = WA, *y = WB, *T; for(i =0; I < m; i + +) wd[i] =0; for(i =0; I < n; i + +) Wd[x[i]=r[i]] + +; for(i =1; I < m; i + +) Wd[i] + = wd[i-1]; for(i = n1; I >=0; I--) sa[--wd[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 + +) wv[i] =X[y[i]]; for(i =0; I < m; i + +) wd[i] =0; for(i =0; I < n; i + +) Wd[wv[i]] + +; for(i =1; I < m; i + +) Wd[i] + = wd[i-1]; for(i = n1; I >=0; I--) sa[--wd[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 + +; } } } //The calculated height [1,n] corresponds to [1,n] in the SA//rank [0,n-1] corresponds to [1,n] in SA//the last 0 added at the time of invocation is not added.voidCalheight (int*r,intN) {//The array of height is evaluated. intI, j, k =0; for(i =1; I <= N; i + +) rank[sa[i]] =i; for(i =0; I < n; height[rank[i++]] =k) { for(K. K--:0, j = sa[rank[i]-1]; R[i+k] = = R[j+k]; K + +); } }/*//In order to unify, all subscript starting from 0, left closed right open! void fix (int n) {rep (i,n) sa[i]=sa[i+1],rank[i]--, height[i]=height[i+1];}*/voidDemo () {intN; SF ("%d",&N); intA[n]; Rep (i,n) SF ("%d",&A[i]); A[n]=0; Da (a,n+1, -); Calheight (a,n);}intMain () {return 0;}
Suffix array template