A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1…88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings.
Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it:
is at least five notes long
appears (potentially transposed – see below) again somewhere else in the piece of music
is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)
Transposed means that a constant positive or negative value is added to every note value in the theme subsequence.
Given a melody, compute the length (number of notes) of the longest theme.
One second time limit for this problem’s solutions!
Input
The input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes.
The last test case is followed by one zero.
Output
For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.
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
Use scanf instead of cin to reduce the read time.
因为height[i]表示的是suffix(sa[i])和suffix(sa[i-1])的公共前缀,而排名相近的字符串公共前缀是相同的,所以height[i]>=k时,这些不间断的i都有相同的长度为k的公共前缀,很多题的解法都是以此为基础的
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rint register int
#define inv inline void
#define ini inline int
#define maxn 20005
using namespace std;
int s[maxn];
int y[maxn], x[maxn], c[maxn], sa[maxn], rk[maxn], height[maxn], wt[30];
int n, m;
void memsetall() {
memset(s, 0, sizeof(s));
memset(y, 0, sizeof(y));
memset(x, 0, sizeof(x));
memset(c, 0, sizeof(c));
memset(sa, 0, sizeof(sa));
memset(rk, 0, sizeof(rk));
memset(height, 0, sizeof(height));
}
inv get_SA()
{
for (rint i = 1; i <= n; ++i) ++c[x[i] = s[i]];
for (rint i = 2; i <= m; ++i) c[i] += c[i - 1];
for (rint i = n; i >= 1; --i) sa[c[x[i]]--] = i;
for (rint k = 1; k <= n; k <<= 1)
{
rint num = 0;
for (rint i = n - k + 1; i <= n; ++i) y[++num] = i;
for (rint i = 1; i <= n; ++i) if (sa[i] > k) y[++num] = sa[i] - k;
for (rint i = 1; i <= m; ++i) c[i] = 0;
for (rint i = 1; i <= n; ++i) ++c[x[i]];
for (rint i = 2; i <= m; ++i) c[i] += c[i - 1];
for (rint i = n; i >= 1; --i) sa[c[x[y[i]]]--] = y[i], y[i] = 0;
swap(x, y);
x[sa[1]] = 1; num = 1;
for (rint i = 2; i <= n; ++i)
x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
if (num == n) break;
m = num;
}
}
inv get_height()
{
rint k = 0;
for (rint i = 1; i <= n; ++i) rk[sa[i]] = i;
for (rint i = 1; i <= n; ++i)
{
if (rk[i] == 1) continue;
if (k) --k;
rint j = sa[rk[i] - 1];
while (j + k <= n && i + k <= n && s[i + k] == s[j + k]) ++k;
height[rk[i]] = k;
}
}
bool exist(int len) {
int mn = n + 1, mx = 0;
for (int i = 1; i <= n; i++) {
if (height[i] < len) {
if (mx - mn > len)return 1;
mx = mn = sa[i];
}
else {
mn = min(mn, sa[i]);
mx = max(mx, sa[i]);
if (mx - mn > len)return 1;
}
}
return 0;
}
int binary_search() {
int l = 0, r = n;
while (l <= r) {
int mid = (l + r) / 2;
if (exist(mid))l = mid + 1;
else r = mid - 1;
}
return r;
}
int main()
{
freopen("shuru.txt", "r", stdin);
while (scanf("%d", &n)!=EOF) {
if (n == 0)break;
memsetall();
for (int i = 1; i <= n; i++)scanf("%d", &s[i]);
for (int i = 2; i <= n; i++)s[i - 1] = (s[i] - s[i - 1] + 88);
n--;
m = 200;
get_SA();
get_height();
int ans = binary_search() + 1;
printf("%d\n", ans < 5 ? 0 : ans);
}
}