Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 33462 | Accepted: 11124 |
Description
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:
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!
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.
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.
Source
題目大意:
給出一個長度為$n$的序列,讓你找出最長的相似子串。這裡相似定義為兩個串每次字元對應的差值相同
Sol:
很顯然,我們可以對原序列進行差分,這樣如果在原序列中長度為$n$的互不相交的相同的字串,那麼答案為$n + 1$
這是一個經典的問題。首先二分答案,然後對$height$陣列分組,若$sa[i] - sa[j] > ans$那麼可以更新答案
#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 1e5 + 10; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N; int s[MAXN], sa[MAXN], rak[MAXN], tp[MAXN], tax[MAXN], height[MAXN], P, M; void Qsort() { for(int i = 0; i <= M; i++) tax[i] = 0; for(int i = 1; i <= N; i++) tax[rak[i]]++; for(int i = 1; i <= M; i++) tax[i] += tax[i - 1]; for(int i = N; i >= 1; i--) sa[ tax[rak[tp[i]]]-- ] = tp[i]; } void SuffixSort() { M = 233; for(int i = 1; i <= N; i++) rak[i] = s[i], tp[i] = i; Qsort(); for(int w = 1, p = 0; p < N; M = p, w <<= 1) { p = 0; for(int i = 1; i <= w; i++) tp[++p] = N - i + 1; for(int i = 1; i <= N; i++) if(sa[i] > w) tp[++p] = sa[i] - w; Qsort(); swap(tp, rak); rak[sa[1]] = p = 1; for(int i = 2; i <= N; i++) rak[sa[i]] = (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w]) ? p : ++p; } int j = 0, k = 0; for(int i = 1; i <= N; i++) { if(k) k--; int j = sa[rak[i] - 1]; while(s[i + k] == s[j + k]) k++; height[rak[i]] = k; } //for(int i = 1; i <= N; i++) printf("%d ", sa[i]); puts(""); } bool check(int len) { int mx = sa[1], mi = sa[1]; for(int i = 2; i <= N; i++) { if(height[i] >= len - 1) mx = max(sa[i], mx), mi = min(sa[i], mi); else mx = mi = sa[i]; if(mx - mi >= len) return 1; } return 0; } int solve() { int l = 0, r = N, ans = 0; while(l <= r) { int mid = l + r >> 1; if(check(mid)) l = mid + 1, ans = mid; else r = mid - 1; } return ans; } int main() { while(scanf("%d", &N) && N != 0) { for(int i = 1; i <= N; i++) s[i] = read(); for(int i = N; i >= 1; i--) s[i] -= s[i - 1] - 100; SuffixSort(); int ans = solve(); if(ans >= 5) printf("%d\n", ans); else printf("%d\n", 0); } return 0; } /* 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 */