The suffix array is twice times more solvable.
1#include <cstdio>2#include <cstring>3#include <cstdlib>4 5 #defineMAXN 10056 #defineINF 0XFFFFF7 #defineMAXM 278 9 intWA[MAXN], WB[MAXN], WS[MAXN], WV[MAXN];Ten CharS[MAXN]; One intSTR[MAXN], SA[MAXN]; A intHEIGHT[MAXN], RANK[MAXN]; - - intMaxintAintb) { the returnA>b?a:b; - } - - intMinintAintb) { + returnA<b?a:b; - } + A intcmpint*r,intAintBintl) { at returnR[A]==R[B] && r[a+l]==r[b+l]; - } - - voidDaint*r,int*sa,intNintm) { - intI, J, *x = WA, *y = WB, *T; - intp; in - for(i=0; i<m; ++i) Ws[i] =0; to for(i=0; i<n; ++i) ws[x[i]=r[i]]++; + for(i=1; i<m; ++i) ws[i] + = ws[i-1]; - for(i=n-1; i>=0; -i) sa[--ws[x[i]] =i; the for(j=1, p=1; j<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;Panax Notoginseng for(i=0; i<n; ++i) Wv[i] =X[y[i]]; - for(i=0; i<m; ++i) Ws[i] =0; the for(i=0; i<n; ++i) ws[wv[i]]++; + for(i=1; i<m; ++i) ws[i] + = ws[i-1]; A for(i=n-1; i>=0; -i) sa[--ws[wv[i]] =Y[i]; the 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)? P1: p++; - } $ } $ - voidCalheight (int*r,int*sa,intN) { - intI, j, k =0; the - for(i=1; i<=n; ++i) Rank[sa[i] =i;Wuyi for(i=0; i<n; height[rank[i++]]=k) the for(K.--k:0, j=sa[rank[i]-1]; R[I+K]==R[J+K]; ++k) - /* do nothing*/; Wu } - About intGetrepeat (intLenintN) { $ intI, MAXX, Minn; - intRET =0; - -Maxx =-1; AMinn =INF; + the for(i=1; i<=n; ++i) { - if(Height[i] >=Len) { $Maxx = Max (Sa[i], Max (sa[i-1], Maxx)); theMinn = min (sa[i], min (sa[i-1], Minn)); the}Else { the if(maxx!=-1&& minn!=inf && (Maxx-minn) >=len) the++ret; -Maxx =-1; inMinn =INF; the } the } About the if(maxx!=-1&& minn!=inf && (Maxx-minn) >=len) the++ret; the + returnret; - } the Bayi voidPrintrank (intN) { the inti; the -printf"Print rank...\n"); - for(i=1; i<=n; ++i) theprintf"%d", Rank[i]); theprintf"\ n"); the } the - voidPrintheight (intN) { the inti; the theprintf"Print height...\n");94 for(i=1; i<=n; ++i) theprintf"%d", Height[i]); theprintf"\ n"); the }98 About voidPrintsa (intN) { - inti;101 102printf"Print sa...\n");103 for(i=0; i<=n; ++i)104printf"%d", Sa[i]); theprintf"\ n");106 }107 108 intMain () {109 intI, Len; the intans;111 the #ifndef Online_judge113Freopen ("data.in","R", stdin); theFreopen ("Data.out","W", stdout); the #endif the 117 while(SCANF ("%s", s)!=eof && (s[0]!='#')) {118 //printf ("%s\n", s);119 for(i=0; S[i]; ++i) -Str[i] = s[i]-'a'+1;121Str[i] =0;122Len =i;123Da (str, SA, len+1, MAXM);124 calheight (str, SA, Len); the #ifndef Online_judge126 Printsa (len);127 Printrank (len); - Printheight (len);129 #endif the for(ans=0, i=1; i<=len/2; ++i) {131Ans + =getrepeat (i, Len); the }133printf"%d\n", ans);134 }135 136 return 0;137}
"Hdoj" 3518 boring counting