poj1743 Musical Theme【解法一】

来源:互联网 发布:淘宝考试 编辑:程序博客网 时间:2024/05/17 21:54

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 longappears (potentially transposed -- see below) again somewhere else in the piece of musicis 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做法见【这里】
先对原数列差分,解决变调的问题。然后问题就变成了求不重叠的最长重复子串。
先求一遍后缀数组,然后二分答案,以height值不到答案的地方为边界分块,这样每一块里任意两个后缀都有长度不小于答案的LCP。检查是否在同一块里有两个的位置之差超过答案即可。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxx=90;int a[20010],t1[20010],t2[20010],cnt[20010],sa[20010],rank[20010],height[20010],n;void init(){    int i,x,last;    scanf("%d",&last);    n--;    for (i=1;i<=n;i++)    {        scanf("%d",&x);        a[i]=x-last+maxx;        last=x;    }}void make(){    int i,j,k,p,m=2*maxx,*x=t1,*y=t2;    for (i=1;i<=m;i++)      cnt[i]=0;    for (i=1;i<=n;i++)      cnt[x[i]=a[i]]++;    for (i=2;i<=m;i++)      cnt[i]+=cnt[i-1];    for (i=n;i;i--)      sa[cnt[x[i]]--]=i;    for (k=1;k<=n;k<<=1)    {        p=0;        for (i=n-k+1;i<=n;i++)          y[++p]=i;        for (i=1;i<=n;i++)          if (sa[i]-k>=1)            y[++p]=sa[i]-k;        for (i=1;i<=m;i++)          cnt[i]=0;        for (i=1;i<=n;i++)          cnt[x[y[i]]]++;        for (i=2;i<=m;i++)          cnt[i]+=cnt[i-1];        for (i=n;i;i--)          sa[cnt[x[y[i]]]--]=y[i];        swap(x,y);        p=1;        x[sa[1]]=1;        for (i=2;i<=n;i++)          if (y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k])            x[sa[i]]=p;          else            x[sa[i]]=++p;        if ((m=p)>=n) break;    }    for (i=1;i<=n;i++)      rank[sa[i]]=i;    for (i=1,k=0;i<=n;i++)    {        if (k) k--;        if (rank[i]==1)        {            k=rank[1]=0;            continue;        }        while (a[i+k]==a[sa[rank[i]-1]+k]) k++;        height[rank[i]]=k;    }}bool ok(int x){    int i,mn,mx;    for (i=2,mx=mn=sa[1];i<=n;i++)      if (height[i]>=x)      {        mx=max(mx,sa[i]);        mn=min(mn,sa[i]);      }      else      {        if (mx-mn>x) return 1;        mx=mn=sa[i];      }    return mx-mn>x;}int main(){    int l,r,mid;    while (scanf("%d",&n)&&n)    {        init();        make();        l=3;        r=n;        while (l<r)        {            mid=(l+r+1)/2;            if (ok(mid)) l=mid;            else r=mid-1;        }        if (l==3) printf("0\n");        else printf("%d\n",l+1);    }}
0 0