Musical Theme
Time Limit: 1000MS | | Memory Limit: 30000K |
Total Submissions: 26471 | | Accepted: 8936 |
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:
- 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
解题思路:
重复2 次以上的子串的个数(不重叠)【单串】
枚举字串长度h , 对于每一次的h,利用height数组,
找出连续的height大于等于h的里面最左端和最右端得为之l和r.
如果l + h - 1 < r的话,说明没有重叠,答案加1.
注意:在找最左端l和最右端r时,假设已经找到了i和j,那么下一次i一定会从j+1开始枚举。因为i-j这一段区间都是符合height>=h的,也就是说这一段区间已经找过了,不需要重复找了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 20005;
int s[maxn],t[maxn],t2[maxn];
int Rank[maxn],height[maxn],sa[maxn];
int n,buc[maxn],a[maxn];
void getsa(int n,int m)
{
int i,*x = t,*y = t2;
for(i = 0; i < m; i++) buc[i] = 0;
for(i = 0; i < n; i++) buc[x[i] = s[i]]++;
for(i = 0; i < m; i++) buc[i] += buc[i-1];
for(i = n-1; i >= 0; i--) sa[--buc[x[i]]] = i;
for(int k = 1; k <= n; k = k << 1)
{
int p = 0;
for(i = n-k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = 0; i < m; i++) buc[i] = 0;
for(i = 0; i < n; i++) buc[x[y[i]]]++;
for(i = 0; i < m; i++) buc[i] += buc[i-1];
for(i = n-1; i >= 0; i--) sa[--buc[x[y[i]]]] = y[i];
swap(x,y);
p = 1, x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1:p++;
if(p >= n) break;
m = p;
}
}
void getheight(int n)
{
int i,j,k = 0;
for(i = 1; i <= n; i++) Rank[sa[i]] = i;
for(i = 0; i < n; i++)
{
if(k) k--;
if(Rank[i] == 0)
{
height[Rank[i]] = 0;
continue;
}
j = sa[Rank[i]-1];
while(s[i+k] == s[j+k]) k++;
height[Rank[i]] = k;
}
}
bool check(int len)
{
int cnt = 0,j = 1;
for(int i = 1; i <= n; i++)
{
j = i;
int l = 0x3f3f3f3f,r = 0;
while(height[j] >= len)
{
r = max(r,max(sa[j-1],sa[j]));
l = min(l,min(sa[j-1],sa[j]));
j++;
}
i = j; //不需要重复找区间,直接i=j
if(l + len - 1 < r) return true;
}
return false;
}
int solve()
{
int l = 1,r = n,mid,ans;
while(l <= r)
{
mid = (l + r) >> 1;
if(check(mid))
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans;
}
int main()
{
int Min;
while(scanf("%d",&n),n)
{
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
for(int i = 0; i < n - 1; i++)
s[i] = a[i+1] - a[i] + 100;
n--;
s[n] = 0;
getsa(n+1,200);
getheight(n);
int ans = solve() + 1;
if(ans < 5) ans = 0;
printf("%d\n",ans);
}
return 0;
}