标签:
aaa abab
6 6
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100010; 4 char s[maxn]; 5 int sa[maxn],rk[maxn],height[maxn],n; 6 int c[maxn],tmp[2][maxn]; 7 void build_sa(int m) { 8 int i,*x = tmp[0],*y = tmp[1]; 9 for(i = 0; i < m; ++i) c[i] = 0; 10 for(i = 0; i < n; ++i) c[x[i] = s[i]]++; 11 for(i = 1; i < m; ++i) c[i] += c[i-1]; 12 for(i = n-1; i >= 0; --i) sa[--c[x[i]]] = i; 13 14 for(int k = 1; k <= n; k <<= 1) { 15 int p = 0; 16 for(i = 0; i < m; ++i) c[i] = 0; 17 for(i = n - k; i < n; ++i) y[p++] = i; 18 for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i]-k; 19 for(i = 0; i < n; ++i) c[x[y[i]]]++; 20 for(i = 1; i < m; ++i) c[i] += c[i-1]; 21 for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i]; 22 swap(x,y); 23 x[sa[0]] = 0; 24 for(p = i = 1; i < n; ++i) 25 if(y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]) 26 x[sa[i]] = p-1; 27 else x[sa[i]] = p++; 28 if((m = p) >= n) break; 29 } 30 } 31 void getHeight() { 32 int i,j,k = 0; 33 for(i = 0; i < n; ++i) rk[sa[i]] = i; 34 for(i = 0; i < n; ++i) { 35 if(k) --k; 36 j = sa[rk[i]-1]; 37 while(i + k < n && j + k < n && s[i+k] == s[j+k]) ++k; 38 height[rk[i]] = k; 39 } 40 } 41 int main() { 42 while(~scanf("%s",s)) { 43 n = strlen(s) + 1; 44 build_sa(128); 45 getHeight(); 46 int pos = rk[0],tmp = n-1,ret = n-1; 47 while(pos + 1 < n && tmp) { 48 tmp = min(tmp,height[++pos]); 49 ret += tmp; 50 } 51 pos = rk[0],tmp = n-1; 52 while(pos > 1 && tmp) { 53 tmp = min(tmp,height[pos--]); 54 ret += tmp; 55 } 56 printf("%d\n",ret%256); 57 } 58 return 0; 59 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4774045.html