poj 1743 Musical Theme
内容
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
Hint
Use scanf instead of cin to reduce the read time.
题解
首先是趋势相同,做个差就是相同了
先考虑求一个出现次数>=2的最长子串
出现次数>=2那就只管|right(x)|>=2的点,然后对这些点的代表的子串长度取max即可
加上了不相交的限制,类似后缀数组的做法,我们就记录right集合的最大/最小值,他们的差值就是能不相交的最长长度,拿它和该点的maxs取min即可,然后对所有点的答案取max即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 20010
using namespace std;
int dis[N<<1],fa[N<<1],ch[N<<1][180],l[N<<1],r[N<<1],a[N],last=1,sz=1,root=1,ans,n;
void insert(int x)
{
int now=++sz,pre=last;last=now;
l[now]=r[now]=dis[now]=dis[pre]+1;
for (;pre && !ch[pre][x];pre=fa[pre]) ch[pre][x]=now;
if (!pre) fa[now]=root;
else if (dis[ch[pre][x]]==dis[pre]+1) fa[now]=ch[pre][x];
else
{
int q=ch[pre][x],nows=++sz;dis[nows]=dis[pre]+1;
memcpy(ch[nows],ch[q],sizeof ch[q]);
fa[nows]=fa[q],fa[q]=fa[now]=nows;
for (;pre && ch[pre][x]==q;pre=fa[pre]) ch[pre][x]=nows;
}
}
int ton[N<<1],ti[N<<1];
void work()
{
for (int i=1;i<=sz;i++) ton[dis[i]]++;
for (int i=1;i<=n;i++) ton[i]+=ton[i-1];
for (int i=1;i<=sz;i++) ti[ton[dis[i]]--]=i;
for (int i=sz;i;i--)
l[fa[ti[i]]]=min(l[fa[ti[i]]],l[ti[i]]),
r[fa[ti[i]]]=max(r[fa[ti[i]]],r[ti[i]]);
for (int i=sz;i;i--)
ans=max(ans,min(dis[i],r[i]-l[i]));
printf("%d\n",ans<4?0:ans+1);
}
void init()
{
memset(dis,0,sizeof dis);
memset(fa,0,sizeof fa);
memset(ch,0,sizeof ch);
memset(l,0x3f,sizeof l);
memset(r,0,sizeof r);
memset(ton,0,sizeof ton);
memset(ti,0,sizeof 0);
ans=0,last=sz=root=1;
}
main()
{
while(~scanf("%d",&n) && n)
{
init();
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<n;i++) a[i]=a[i+1]-a[i]+88;
n--;
for (int i=1;i<=n;i++) insert(a[i]);
work();
}
}