Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 27539   Accepted: 9290

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.

Source


不重叠最长重复子串
冷静的说一下做法:
1.差分之后体现的是变化率,解决可以+-问题(就像是波形一样)  【注意原序列相差满足x 差分序列相差满足x+1 因为差分后每个元素代表两个元素,得到的结果长度至少是2 如1 2 3-->1 1不行】
2.不重叠的重复子串,也是二分最长长度然后分组,同一组内height满足了最长长度的要求一组内sa值相差问题(因为sa[i]表示了位置),mx-mn>=k的话可以
 
我靠什么鬼.........................该死该死该死该死什么问题啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
难道是后缀数组的问题?不能啊
该死(曾经这里有几十行这个词...)
 
【2016-12-28 00:15:57】
发现问题了,傻逼的对于n=1特判结果后面的一个数没读入..........
实践证明我的模板是对的
 

【2017-02-06】

除了分组还有一种做法,从大到小枚举L然后合并 维护并查集内mx和mn看看是不是>L(注意并查集用的编号是排序后的排名)

为什么>L 因为 1 2 3-->1 1 两个其实是重合的

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2e4+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,k;
int s[N];
int sa[N],c[N],t1[N],t2[N],height[N],rnk[N]; void getHeight(){
int k=;
for(int i=;i<=n;i++) rnk[sa[i]]=i;
for(int i=;i<=n;i++){
if(k) k--;
if(rnk[i]==) continue;
int j=sa[rnk[i]-];
while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
height[rnk[i]]=k;
}
}
inline bool cmp(int *r,int a,int b,int j){
return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];
}
void getSA(){
m=;
int *r=t1,*k=t2;
for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[i]=s[i]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[i]]--]=i; for(int j=;j<=n;j<<=){
int p=;
for(int i=n-j+;i<=n;i++) k[++p]=i;
for(int i=;i<=n;i++) if(sa[i]>j) k[++p]=sa[i]-j; for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[k[i]]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[k[i]]]--]=k[i]; swap(r,k);p=;r[sa[]]=++p;
for(int i=;i<=n;i++) r[sa[i]]=cmp(k,sa[i],sa[i-],j)?p:++p;
if(p>=n) break;m=p;
}
} struct edge{
int v,ne;
}e[N];
int h[N],cnt;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
int fa[N],mn[N],mx[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void unn(int x,int y){
x=find(x);y=find(y);
if(x!=y){
fa[x]=y;
mn[y]=min(mn[y],mn[x]);
mx[y]=max(mx[y],mx[x]);
}
}
void solve(){
cnt=;memset(h,,sizeof(h));
for(int i=;i<=n;i++){
fa[i]=i,mn[i]=mx[i]=sa[i];
ins(height[i],i);
}
for(int L=n-;L>=;L--){
for(int i=h[L];i;i=e[i].ne) unn(e[i].v,e[i].v-);
if(h[L]){
int x=find(e[h[L]].v);
if(mx[x]-mn[x]>L) {printf("%d\n",L+);return;}
}
}
puts("");
}
int main(){
freopen("in","r",stdin);
while((n=read())){
n--;
int last=read(),x;
for(int i=;i<=n;i++){
x=read();
s[i]=x-last+;
last=x;
}
getSA();
getHeight();
solve();
}
}

SA+并查集

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2e4+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,k;
int s[N];
int sa[N],c[N],t1[N],t2[N];
inline bool cmp(int *r,int a,int b,int j){
return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];
}
int rnk[N],height[N];
void getHeight(){
int k=;
for(int i=;i<=n;i++) rnk[sa[i]]=i;
for(int i=;i<=n;i++){
if(k) k--;
if(rnk[i]==) continue;
int j=sa[rnk[i]-];
while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
height[rnk[i]]=k;
}
}
void buildSA(){
int *r=t1,*y=t2;
for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[i]=s[i]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[i]]--]=i; for(int j=;j<=n;j<<=){
int p=;
for(int i=n-j+;i<=n;i++) y[++p]=i;
for(int i=;i<=n;i++) if(sa[i]>j) y[++p]=sa[i]-j; for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[y[i]]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[y[i]]]--]=y[i]; swap(r,y);p=;r[sa[]]=++p;
for(int i=;i<=n;i++) r[sa[i]]=cmp(y,sa[i],sa[i-],j)?p:++p;
if(p>=n) break;m=p;
}
getHeight();
} bool check(int mid){
int mn=sa[],mx=sa[];
for(int i=;i<=n;i++){
if(height[i]>=mid){
mn=min(mn,sa[i]);
mx=max(mx,sa[i]);
if(mx-mn>mid) return true;
}else mn=mx=sa[i];
}
return false;
}
void solve(){
int l=,r=n>>,ans=;
while(l<=r){
int mid=(l+r)>>;
if(check(mid)) ans=mid,l=mid+;
else r=mid-;
}
if(ans+<) puts("");
else printf("%d\n",ans+);
}
int main(){
//freopen("in.txt","r",stdin);
while((n=read())){
n--;m=;
int last=read(),x;
for(int i=;i<=n;i++){
x=read();
s[i]=x-last+;
last=x;
}
buildSA();
solve();
}
}
 
 

POJ1743 Musical Theme [后缀数组+分组/并查集]的更多相关文章

  1. POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Tot ...

  2. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  3. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

  4. POJ-1743 Musical Theme(后缀数组)

    题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...

  5. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

  6. [Poj1743] [后缀数组论文例题] Musical Theme [后缀数组不可重叠最长重复子串]

    利用后缀数组,先对读入整数处理str[i]=str[i+1]-str[i]+90这样可以避免负数,计算Height数组,二分答案,如果某处H<lim则将H数组分开,最终分成若干块,判断每块中是否 ...

  7. [poj 1743] Musical Theme 后缀数组 or hash

    Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...

  8. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

  9. poj 1743 Musical Theme (后缀数组+二分法)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16162   Accepted: 5577 De ...

随机推荐

  1. P2889 [USACO07NOV]挤奶的时间Milking Time

    P2889 [USACO07NOV]挤奶的时间Milking Time 奶牛Bessie在0~N时间段产奶.农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e.奶牛产奶后需要休息 ...

  2. Java基础-SSM之mybatis一对一外键关联

    Java基础-SSM之mybatis一对一外键关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表)  1>.创建husbandsfk和wife ...

  3. 函数和常用模块【day04】: 总结(十二)

  4. bzoj千题计划195:bzoj2844: albus就是要第一个出场

    http://www.lydsy.com/JudgeOnline/problem.php?id=2844 题意:给定 n个数,把它的所有子集(可以为空)的异或值从小到大排序得到序列 B,请问 Q 在  ...

  5. prefab内容分析

    写在前面: 当前使用的unity版本:5.3.7p4. 如果打开prefab文件是乱码: 把editer的asset Srialization改为Force Text即可. 一.什么是Prefab P ...

  6. 略显犀利的 js 判断闰年

    /** * 判断闰年函数 * @param {number} year 要判断的年份 * @return {bool} 返回布尔值 * * 其实只要满足下面几个条件即可. * 1.普通年能被4整除且不 ...

  7. CSS-3 box-shadow 的使用

    box-shadow是给对象实现图层阴影效果的. 语法: E {box-shadow: <length> <length> <length>?<length& ...

  8. oracle用户密码过期!the password has expired

    Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...

  9. 第9月第5天 AVVideoAverageBitRateKey

    1. https://stackoverflow.com/questions/11751883/how-can-i-reduce-the-file-size-of-a-video-created-wi ...

  10. FinalShell 推荐

    FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求. 用户QQ群 342045988 Windows版下载地址:http:/ ...