博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1743 Musical Theme
阅读量:6182 次
发布时间:2019-06-21

本文共 3798 字,大约阅读时间需要 12 分钟。

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 28409   Accepted: 9591

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

3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800

Sample Output

5

Hint

Use scanf instead of cin to reduce the read time.

Source

题意:
求两个最长相似子串(差分序列相同)的长度
/*得出height数组后,二分答案x,将连续的>=x的段分组如果一组内sa的最大值与最小值的差>=x,则x成立*/#include
#include
#include
using namespace std;const int N=2e4+5;int n,sa[N],tsa[N],rank[N],trank[N],c[N],h[N];int s[N];void DA(int maxx=256){ memset(c,0,sizeof c);int p; for(int i=1;i<=n;i++) c[rank[i]=s[i]]++; for(int i=2;i<=maxx;i++) c[i]+=c[i-1]; for(int i=n;i;i--) sa[c[rank[i]]--]=i; trank[sa[1]]=p=1; for(int i=2;i<=n;i++){ if(rank[sa[i]]!=rank[sa[i-1]]) p++; trank[sa[i]]=p; } for(int i=1;i<=n;i++) rank[i]=trank[i]; for(int k=1;p
<<=1,maxx=p){ p=0; for(int i=n-k+1;i<=n;i++) tsa[++p]=i; for(int i=1;i<=n;i++) if(sa[i]>k) tsa[++p]=sa[i]-k; memset(c,0,sizeof c); for(int i=1;i<=n;i++) trank[i]=rank[tsa[i]]; for(int i=1;i<=n;i++) c[trank[i]]++; for(int i=2;i<=maxx;i++) c[i]+=c[i-1]; for(int i=n;i;i--) sa[c[trank[i]]--]=tsa[i]; trank[sa[1]]=p=1; for(int i=2;i<=n;i++){ if(rank[sa[i]]!=rank[sa[i-1]]||rank[sa[i]+k]!=rank[sa[i-1]+k]) p++; trank[sa[i]]=p; } for(int i=1;i<=n;i++) rank[i]=trank[i]; } for(int i=1,k=0;i<=n;i++){ int j=sa[rank[i]-1]; while(s[i+k]==s[j+k]) k++; h[rank[i]]=k;if(k>0) k--; }}bool judge(int k){ int mn=sa[1],mx=sa[1]; for(int i=2;i<=n;i++){ if(h[i]
=k) return 1; } } return 0;}void Clear(){ memset(h,0,sizeof h); memset(sa,0,sizeof sa); memset(tsa,0,sizeof tsa); memset(rank,0,sizeof rank); memset(trank,0,sizeof trank);}int main(){ while(scanf("%d",&n)==1){ if(!n) break; Clear(); for(int i=1;i<=n;i++) scanf("%d",&s[i]);n--; for(int i=1;i<=n;i++) s[i]=s[i+1]-s[i]+100; DA(); int l=0,r=n/2,mid,ans=0; while(l<=r){ mid=l+r>>1; if(judge(mid)) l=mid+1,ans=mid; else r=mid-1; } ans++; if(ans<5) ans=0; printf("%d\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/shenben/p/6589900.html

你可能感兴趣的文章
web前端性能优化总结
查看>>
玩转小程序转发——小程序探索
查看>>
【基础】小程序实现聊天气泡样式
查看>>
Docker入门(三)使用Docker Compose
查看>>
CDN知识详解
查看>>
Python爬虫:学了requests库和re库之后能做的事情
查看>>
天下无难试之HashMap面试刁难大全
查看>>
IP地址自动封与解封的shell脚本
查看>>
ubuntu 系统环境配置文件的区别
查看>>
精通visual c++指纹模式识别系统算法及实现
查看>>
博客园自定义页面风格设计 后续篇(页面设计模式及代码高亮 鼠标点击效果升级)...
查看>>
[知识盲点] 为function添加值为function的属性
查看>>
Emacs for Windows use TRAMP
查看>>
ssh登录的调试方法和常见问题
查看>>
PHP 7.3 比 PHP 7.0 快 22%,即将进入特性冻结阶段
查看>>
Java 9 文章集锦
查看>>
案例丨数据驱动的发现页最低成本改版
查看>>
普通用户竟这样执行xp_cmdshell存储过程!
查看>>
Visualize Famous Campus in China
查看>>
C#给PDF文档添加文本和图片页眉
查看>>