地址:http://poj.org/problem?id=1743
题目:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 28675 | Accepted: 9674 |
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
Source
1 #include <cstdlib>
2 #include <cstring>
3 #include <cstdio>
4 #include <algorithm>
5
6 const int N = 20005;
7 int sa[N],s[N],wa[N], wb[N], ws[N], wv[N];
8 int rank[N], height[N];
9
10 bool cmp(int r[], int a, int b, int l)
11 {
12 return r[a] == r[b] && r[a+l] == r[b+l];
13 }
14
15 void da(int r[], int sa[], int n, int m)
16 {
17 int i, j, p, *x = wa, *y = wb;
18 for (i = 0; i < m; ++i) ws[i] = 0;
19 for (i = 0; i < n; ++i) ws[x[i]=r[i]]++;
20 for (i = 1; i < m; ++i) ws[i] += ws[i-1];
21 for (i = n-1; i >= 0; --i) sa[--ws[x[i]]] = i;
22 for (j = 1, p = 1; p < n; j *= 2, m = p)
23 {
24 for (p = 0, i = n - j; i < n; ++i) y[p++] = i;
25 for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
26 for (i = 0; i < n; ++i) wv[i] = x[y[i]];
27 for (i = 0; i < m; ++i) ws[i] = 0;
28 for (i = 0; i < n; ++i) ws[wv[i]]++;
29 for (i = 1; i < m; ++i) ws[i] += ws[i-1];
30 for (i = n-1; i >= 0; --i) sa[--ws[wv[i]]] = y[i];
31 for (std::swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)
32 x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;
33 }
34 }
35
36 void calheight(int r[], int sa[], int n)
37 {
38 int i, j, k = 0;
39 for (i = 1; i <= n; ++i) rank[sa[i]] = i;
40 for (i = 0; i < n; height[rank[i++]] = k)
41 for (k?k--:0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++);
42 }
43 bool check(int len,int x)
44 {
45 int mx=sa[1],mi=sa[1];
46 for(int i=2;i<=len;i++)
47 {
48 if(height[i]<x)
49 {
50 if(mx-mi>=x)
51 return 1;
52 mx=mi=sa[i];
53 }
54 else
55 {
56 mx=std::max(sa[i],mx);
57 mi=std::min(sa[i],mi);
58 }
59 }
60 return mx-mi>=x;
61 }
62 int main()
63 {
64 int n;
65 while(scanf("%d",&n)&&n)
66 {
67 int x,ls,len=0;
68 scanf("%d",&ls);
69 for(int i=0;i<n-1;i++)
70 scanf("%d",&x),s[len++]=x-ls+100,ls=x;
71 s[len]=0;
72 da(s,sa,len+1,200);
73 calheight(s,sa,len);
74 int l=5,r=n/2,ans=0;
75 while(l<=r)
76 {
77 int mid=l+r>>1;
78 if(check(len,mid-1))
79 ans=mid,l=mid+1;
80 else
81 r=mid-1;
82 }
83 printf("%d\n",ans);
84 }
85 return 0;
86 }
原文:http://www.cnblogs.com/weeping/p/6648714.html