Personal feeling suffix array of the board is too difficult to back, listen to the small train speak two points + hash can be implemented to find a suffix array, seemingly very good understanding, the code is as follows.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <algorithm>7#include <queue>8#include <vector>9 using namespacestd;TentypedefLong LongLL; Onetypedef unsignedLong LongULL; A Const intmaxn=200000; - ConstULL base=127; - CharS[MAXN]; the intSA[MAXN],RANK[MAXN]; -ULL A[MAXN],Base[Maxn],hash[maxn],len; - structh{ - intPos; + }SUF[MAXN]; -Inline ULL Get_hash (intLintR) { + returnhash[r]-hash[l-1]*Base[r-l+1]; A } atInlineintFindintLintRintF1,intF2) { - if(L +1>=R) { - if(Get_hash (f1,f1+r-1) ==get_hash (f2,f2+r-1))returnR; - Else returnl; - } - intMid= (l+r) >>1; in if(Get_hash (f1,f1+mid-1) ==get_hash (f2,f2+mid-1))returnfind (MID,R,F1,F2); - Else returnFind (l,mid-1, F1,F2); to } +InlineBOOLcmpConstH &x,ConstH &y) { - intlen1=len-x.pos+1, len2=len-y.pos+1; the intMaxx=find (0, min (len1,len2) +1, X.pos,y.pos); * if(Maxx==min (LEN1,LEN2) +1)returnlen1<Len2; $ Else returna[x.pos+maxx]<a[y.pos+Maxx];Panax Notoginseng } - intHEIGHT[MAXN]; theInlinevoidcalheight () { + intk=0; A for(intI=1; i<=len;i++){ the if(k!=0) k--; + for(intj=sa[rank[i]-1];; k++){ - if(A[i+k]!=a[j+k]) Break; $ } $height[rank[i]]=K; - } - return ; the } - intMain () {Wuyiscanf"%s", s+1); Len=strlen (s+1); the for(intI=1; i<=len;i++) A[i]=ull (s[i]-'a'+1); - Base[0]=1; for(intI=1; i<=len;i++)Base[i]=Base[I1]*BASE; Wu for(intI=1; i<=len;i++) hash[i]=hash[i-1]*base+A[i]; - for(intI=1; i<=len;i++){ Aboutsuf[i].pos=i; $ } -Sort (suf+1, suf+len+1, CMP); - for(intI=1; i<=len;i++){ -Sa[i]=suf[i].pos; rank[suf[i].pos]=i; A } + calheight (); the for(intI=1; i<=len;i++){ -cout<" "; $ } the return 0; the}
Using a binary + hash to find the suffix array