poj1743 Musical Theme【解法一】
2016-11-27 14:35
225 查看
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!
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。检查是否在同一块里有两个的位置之差超过答案即可。
(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做法见【这里】。
先对原数列差分,解决变调的问题。然后问题就变成了求不重叠的最长重复子串。
先求一遍后缀数组,然后二分答案,以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); } }
相关文章推荐
- poj 1743 Musical Theme(SAM解法)
- poj1743 Musical Theme【解法二】
- POJ 1743 Musical Theme(后缀数组)
- POJ 1743 Musical Theme(2.2 不可重叠最长重复子串)
- poj_1743_Musical Theme(后缀数组)
- POJ1743 Musical Theme,后缀数组,最长重复不重叠子串,二分检索答案
- poj 1743 Musical Theme(后缀数组)
- (Relax 后缀数组1.1)POJ 1743 Musical Theme(后缀数组的基本使用)
- poj 1743 Musical Theme(后缀数组)
- 【POJ 1743】Musical Theme【后缀数组】
- poj 1743 Musical Theme(不可重叠最长重复子串)
- poj 1743 Musical Theme(后缀数组+二分)
- poj1743 Musical Theme(后缀数组|后缀自动机)
- POJ 1743 Musical Theme(后缀数组[不可重叠最长重复子串])
- poj 1743 Musical Theme(后缀自动机)
- POJ-1743-Musical Theme(后缀数组)
- poj1743:Musical Theme(后缀数组)
- POJ 题目1743 Musical Theme(后缀数组,求一个串中最长不重叠重复子串)
- [POJ 1743]Musical Theme(后缀数组)
- poj 1743 Musical Theme (不可重叠最长重复子串)