您的位置:首页 > 其它

poj1743 Musical Theme【解法二】

2017-03-30 20:17 141 查看
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.

后缀数组解法见【这里】

差分以后问题转化为不重叠重复子串长【实际上中间还要隔一个,但是原理影响不大】。建出sam之后按照parent树拓扑序更新,每个节点维护right集合的最大值和最小值,对答案的贡献就是min(maxi−mini−1,vali)。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int con=88,sigma=175,oo=0x3f3f3f3f;
int a[40010],fa[40010],trans[40010][180],val[40010],mx[40010],mn[40010],
cnt[40010],f[40010],
n,tot;
void solve()
{
int p,q,np,nq,last=1,ans=0;
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
if (n==1)
{
printf("0\n");
return;
}
for (int i=1;i<n;i++) a[i]=a[i+1]-a[i]+con;
tot=1;
fa[1]=val[1]=mx[1]=0;
mn[1]=oo;
for (int i=1;i<=sigma;i++) trans[1][i]=0;
for (int i=1;i<n;i++)
{
p=last;
last=np=++tot;
for (int j=1;j<=sigma;j++) trans[np][j]=0;
mx[np]=0;
mn[np]=oo;
val[np]=val[p]+1;
while (p&&!trans[p][a[i]])
{
trans[p][a[i]]=np;
p=fa[p];
}
if (!p) fa[np]=1;
else
{
q=trans[p][a[i]];
if (val[q]==val[p]+1) fa[np]=q;
else
{
nq=++tot;
mx[nq]=0;
mn[nq]=oo;
val[nq]=val[p]+1;
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
for (int j=1;j<=sigma;j++) trans[nq][j]=trans[q][j];
while (p&&trans[p][a[i]]==q)
{
trans[p][a[i]]=nq;
p=fa[p];
}
}
}
}
for (int i=0;i<n;i++) cnt[i]=0;
for (int i=1;i<=tot;i++) cnt[val[i]]++;
for (int i=1;i<n;i++) cnt[i]+=cnt[i-1];
for (int i=1;i<=tot;i++) f[cnt[val[i]]--]=i;
p=1;
for (int i=1;i<n;i++)
{
p=trans[p][a[i]];
mn[p]=mx[p]=i;
}
for (int i=tot;i;i--)
{
ans=max(ans,min(mx[f[i]]-mn[f[i]]-1,val[f[i]]));
mx[fa[f[i]]]=max(mx[fa[f[i]]],mx[f[i]]);
mn[fa[f[i]]]=min(mn[fa[f[i]]],mn[f[i]]);
}
printf("%d\n",ans>=4?ans+1:0);
}
int main()
{
while (scanf("%d",&n)&&n) solve();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SAM