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. Java基础-比较运算符Compare Operators

    Java基础-比较运算符Compare Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关系运算符 关系运算符包括: 1>.大于(>) 2> ...

  2. js监听浏览器tab窗口切换

    js监听浏览器tab窗口切换 ——IT唐伯虎 摘要:js监听浏览器tab窗口切换. if (document.hidden !== undefined) {  document.addEventLis ...

  3. bzoj千题计划257:bzoj4199: [Noi2015]品酒大会

    http://www.lydsy.com/JudgeOnline/problem.php?id=4199 求出后缀数组的height 从大到小枚举,合并 维护组内 元素个数,最大.次大.最小.次小 # ...

  4. hdu 6166 Senior Pan

    http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意: 给出一张无向图,给定k个特殊点 求这k个特殊点两两之间的最短路 二进制分组 枚举一位二进制位 这一 ...

  5. (32位)本体学习程序(ontoEnrich)系统使用说明文档

    系统运行:文件夹system下,可执行文件ontoEnrichment --------------------------------------------------------1.简单概念学习 ...

  6. TTPRequest 提示#import <libxml/HTMLparser.h>找不到 的解决方法

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/3984251.html ,转载请注明出处. ASIHTTPRequest 或者AFNetwork提示的#impo ...

  7. [BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)

    [BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yu ...

  8. 【ORACLE】oracl基本操作笔记

    1.用命令导入导出表 C:\Users\xiang>imp bjlims/bjlims@orcl file="c:\tjlims.dmp" full=y C:\Users\x ...

  9. sync 解释

    sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...

  10. 解决getJSON跨域登录Session丢失的问题

    最近在做项目中发现,我用下面的代码异步请求到login.ashx: var memberUrl = rooturl + 'member.ashx?r=' + Math.random() + '& ...