标签:
30 25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18 82 78 74 70 66 67 64 60 65 80 0
5
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 500010; 6 int sa[maxn],rk[maxn],height[maxn],c[maxn]; 7 int n,t[maxn],t2[maxn],s[maxn]; 8 void build_sa(int m) { 9 int i,j,*x = t,*y = t2; 10 for(i = 0; i < m; ++i) c[i] = 0; 11 for(i = 0; i < n; ++i) c[x[i] = s[i]]++; 12 for(i = 1; i < m; ++i) c[i] += c[i-1]; 13 for(i = n-1; i >= 0; --i) sa[--c[x[i]]] = i; 14 15 for(int k = 1; k <= n; k <<= 1) { 16 int p = 0; 17 for(i = 0; i < m; ++i) c[i] = 0; 18 for(i = n-k; i < n; ++i) y[p++] = i; 19 for(i = 0; i < n; ++i) 20 if(sa[i] >= k) y[p++] = sa[i] - k; 21 for(i = 0; i < n; ++i) c[x[y[i]]]++; 22 for(i = 1; i < m; ++i) c[i] += c[i-1]; 23 for(i = n-1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i]; 24 25 swap(x,y); 26 p = 1; 27 x[sa[0]] = 0; 28 for(i = 1; i < n; ++i) 29 if(y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]) 30 x[sa[i]] = p-1; 31 else x[sa[i]] = p++; 32 if(p >= n) break; 33 m = p; 34 } 35 } 36 void getHeight() { 37 int i,j,k = 0; 38 for(i = 0; i < n; ++i) rk[sa[i]] = i; 39 for(i = 0; i < n; ++i) { 40 if(k) --k; 41 j = sa[rk[i]-1]; 42 while(i + k < n && j + k < n && s[i+k] == s[j+k]) k++; 43 height[rk[i]] = k; 44 } 45 } 46 bool check(int k) { 47 int low = sa[1],high = sa[1]; 48 for(int i = 2; i < n; ++i) { 49 if(height[i] < k) low = high = sa[i]; 50 else { 51 low = min(sa[i],low); 52 high = max(sa[i],high); 53 if(high - low > k) return true; 54 } 55 } 56 return false; 57 } 58 int tmp[maxn]; 59 int main() { 60 while(scanf("%d",&n),n) { 61 for(int i = 0; i < n; ++i) 62 scanf("%d",tmp+i); 63 for(int i = 0; i+1 < n; ++i) 64 s[i] = tmp[i+1] - tmp[i] + 90; 65 s[n-1] = 0; 66 build_sa(200); 67 getHeight(); 68 int low = 4,high = n,ret = -1; 69 while(low <= high) { 70 int mid = (low + high)>>1; 71 if(check(mid)) { 72 low = mid + 1; 73 ret = mid; 74 } else high = mid - 1; 75 } 76 printf("%d\n",ret+1); 77 } 78 return 0; 79 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4634145.html