Original title Link:
Exercises
Just expand the string one more times and then run a suffix array.
Code:
#include <iostream>#include<algorithm>#include<cstdio>#defineINF 2*0x3f3f3f3fusing namespacestd;Const intMAXN =200051;//note the size of the array, and remember to changeintWA[MAXN], WB[MAXN], WV[MAXN], WSS[MAXN];intcmpint*r,intAintBintl) {returnR[a] = = R[b] && r[a + l] = = R[b +l];}voidDaint*r,int*sa,intNintm) {//R for the converted string SA to be evaluated the SA array n is the maximum number of arrays of size +1 m for the array +1 intI, J, p, *x = WA, *y = WB, *T; for(i =0; I < m; i++) Wss[i] =0; for(i =0; I < n; i++) Wss[x[i] = r[i]]++; for(i =1; I < m; i++) wss[i] + = wss[i-1]; for(i = n-1; I >=0; i--) Sa[--wss[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++) Wss[i] =0; for(i =0; I < n; i++) wss[wv[i]]++; for(i =1; I < m; i++) wss[i] + = wss[i-1]; for(i = n-1; I >=0; i--) Sa[--wss[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++; } return;}intRANKING[MAXN], HEIGHT[MAXN];voidBuild_height (int(RNint*sa,intN) {//R is the converted original array, SA is the evaluated array, and N is Len intI, j, k =0; for(i =1; I <= N; i++) Ranking[sa[i] =i; for(i =0; I < n; height[ranking[i++]] =k) { for(K. k--:0, j = Sa[ranking[i]-1]; R[i + K] = = R[j + K]; k++); }}intSA[MAXN], R[MAXN];strings;intSTR[MAXN];intMain () {//freopen ("1031.in", "R", stdin); //freopen ("1031.out", "w", stdout); //Cin.sync_with_stdio (false);CIN >>s; intn =s.length (); S= S +s; for(inti =0; I < s.length (); i++) Str[i] = S[i] +2; Str[s.length ()]=1; Da (str, SA, s.length ()+1, the); for(inti =0; I <= s.length (); i++) { if(sa[i]>=n)Continue; intp = Sa[i] + N-1; cout<<S[p]; } cout<<Endl; return 0;}
Bzoj 1031: [JSOI2007] character encryption cipher suffix array