An array of suffixes to solve the string problem of the advantageous tool, the topic code for the multiplication SA algorithm
Detailed explanation See 2009 National Training Team paper
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstdlib>5#include <cstring>6#include <cmath>7#include <ctime>8 9 using namespacestd;Ten One intstr[110000]; A intbarrel[2][110000],u[110000],tmp[110000],sa[110000],h[110000]; - Charr[110000]; - the voidCalc_h (Const int& N,Const int*Rank) - { - inti,j,k=0; - for(i=0;i<n; h[rank[i++]]=k) + for(k?k--:0, j=sa[rank[i]-1];str[i+k]==str[j+k];++k); - return ; + } A at BOOLcmpConst int* S,Const intAConst intBConst intl) - { - returnS[A]==S[B] && s[a+l]==s[b+l]; - } - - int* GET_SA (Const int& N,intm) in { - inti,j,p,*x=barrel[0],*y=barrel[1]; tomemset (U,0,sizeof(U)); + for(i=0; i<n;++i) u[x[i]=str[i]]++; - for(i=1; i<m;++i) u[i]+=u[i-1]; the for(i=n-1; i>=0;-I.) sa[--u[x[i]]]=i; * $ for(j=1, p=1;p <n;j<<=1, m=p)Panax Notoginseng { - for(p=0, I=n-j;i<n;++i) y[p++]=i; the for(i=0; i<n;++i)if(SA[I]>=J) y[p++]=sa[i]-J; + for(i=0; i<n;++i) tmp[i]=X[y[i]]; A for(i=0; i<m;++i) u[i]=0; the for(i=0; i<n;++i) u[tmp[i]]++; + for(i=1; i<m;++i) u[i]+=u[i-1]; - for(i=n-1; i>=0;-I.) sa[--u[tmp[i]]]=Y[i]; $ for(Swap (x, y), p=1, x[sa[0]]=0, i=1; i<n;++i) $X[SA[I]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; - } - Calc_h (n,x); the returnx; - }Wuyi the intMain () - { Wu intI,n; -scanf"%s", R); Aboutn=strlen (r); $ for(i=0; i<n;++i) str[i]=R[i]; - -Get_sa (n+1, the); - A for(i=1; i<=n;++i) printf ("%d", sa[i]+1); +printf"\ n"); the for(i=2; i<=n;++i) printf ("%d", H[i]); - $ return 0; the}
View Code
In the calculation of the H array, it is not necessary to recalculate because the array in which X is represented in the program that computes the SA array is the rank array.
[Uoj#35] [uoj suffix array template title] suffix sort [suffix array template]