标签:blog i++ -o print rom first return repr sea
Description
Input
Output
Sample Input
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
Sample Output
5
Hint
#include <iostream> #include <cstring> #include <cstdio> const int N = 20000 + 7; using namespace std; int n,r[N],wb[N],wss[N],wv[N],sa[N],rank[N],li[N]; int cmp(int *r,int a,int b,int l) { return r[a] == r[b] && r[a+l] == r[b+l]; } void Da(int n,int m) { int i,j,p,*x = rank, *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 = p = 1; p < n; j <<= 1, 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 = i = 1, x[sa[0]] = 0; i < n; ++i) x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++; } } int f[N], hi[N]; void cal_height( ) { int i,j,k = 0; for(i = 1; i <= n; ++i) f[sa[i]] = i; for(i = 0; i < n; hi[f[i++]] = k) for(k?k--:0,j = sa[f[i]-1]; r[i+k] == r[j+k]; ++k); } bool check(int len) { int minx = n + 1 , maxx = -1,i; for(i = 2; i <= n; ++i) { if(hi[i] >= len) { minx = min(minx,min(sa[i],sa[i-1])); maxx = max(maxx,max(sa[i],sa[i-1])); } else { if(minx + len < maxx) return true; minx = n + 1, maxx = -1; } } if(minx + len < maxx) return true; return false; } int binary_search( ) { int L = 1, R = ( n >> 1 )+ 1, mid, ret = -1; while(L <= R) { mid = ( L + R ) >> 1; if(check(mid)) L = mid + 1, ret = max(ret , mid); else R = mid - 1; } return ret; } int main() { while(~scanf("%d",&n) && n) { int i; memset(r,0,sizeof(r)); for(i = 0; i < n; ++i) scanf("%d",r+i); for(i = 0; i < n; ++i) r[i] = r[i+1] - r[i] + 100; ; n--; r[n] = 0; Da(n+1,201); cal_height( ); int ans = binary_search()+1; if( ans < 5) puts("0"); else printf("%d\n",ans); } return 0; }
标签:blog i++ -o print rom first return repr sea
原文地址:http://www.cnblogs.com/Ateisti/p/6500353.html