poj 1743 后缀数组 最长不重叠子串
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 30941 | Accepted: 10336 |
Description
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 last test case is followed by one zero.
Output
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
Source
题意:
给出长度是n的数组表示音符,求最长的相同音乐段长度,相同音乐段是指两段的变化是一样的(即差值数组都一样)。
代码:
//求最长不重叠子串,把给出的数组转换成差值的形式。论文题就不多说了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
const int INF=0x7fffffff;
int he[MAXN+],ra[MAXN+],sa[MAXN+],xx[MAXN+],yy[MAXN+],buc[MAXN+];
int s[MAXN+],a[MAXN+];
int len,m;
void get_suf()
{
int *x=xx,*y=yy;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[i]=s[i]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[i]]]=i;
for(int k=;k<=len;k<<=){
int p=;
for(int i=len-;i>=len-k;i--) y[p++]=i;
for(int i=;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[y[i]]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(int i=;i<len;i++){
if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
x[sa[i]]=p-;
else x[sa[i]]=p++;
}
if(p>=len) break;
m=p;
}
for(int i=;i<len;i++) ra[sa[i]]=i;
int k=;
for(int i=;i<len;i++){
if(ra[i]==) { he[]=; continue; }
if(k) k--;
int j=sa[ra[i]-];
while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
he[ra[i]]=k;
}
}
bool solve(int mid)
{
int min_sa=INF,max_sa=-INF;
for(int i=;i<len;i++){
if(he[i]>=mid){
min_sa=min(min_sa,min(sa[i],sa[i-]));
max_sa=max(max_sa,max(sa[i],sa[i-]));
if(min_sa+mid-<max_sa) return ;
}else{
min_sa=INF;max_sa=-INF;
}
}
return ;
}
int main()
{
int n;
while(scanf("%d",&n)&&n){
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++) s[i-]=a[i]-a[i-]+;
len=n-;
m=;
get_suf();
int l=,r=len,ans=;
while(l<=r){
int mid=(l+r)>>;
if(solve(mid)) { ans=mid;l=mid+; }
else r=mid-;
}
if(++ans<) ans=;
printf("%d\n",ans);
}
return ;
}
poj 1743 后缀数组 最长不重叠子串的更多相关文章
- POJ 3261 Milk Patterns 【后缀数组 最长可重叠子串】
题目题目:http://poj.org/problem?id=3261 Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Subm ...
- POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
- POJ 1743 (后缀数组+不重叠最长重复子串)
题目链接: http://poj.org/problem?id=1743 题目大意:楼教主の男人八题orz.一篇钢琴谱,每个旋律的值都在1~88以内.琴谱的某段会变调,也就是说某段的数可以加减一个旋律 ...
- POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )
题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 : 根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...
- poj 1743 后缀数组 求最长不重叠重复子串
题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- POJ 1743 后缀数组
题目链接:http://poj.org/problem?id=1743 题意:给定一个钢琴的音普序列[值的范围是(1~88)],现在要求找到一个子序列满足 1,长度至少为5 2,序列可以转调,即存在两 ...
- POJ 2217 (后缀数组+最长公共子串)
题目链接: http://poj.org/problem?id=2217 题目大意: 求两个串的最长公共子串,注意子串是连续的,而子序列可以不连续. 解题思路: 后缀数组解法是这类问题的模板解法. 对 ...
- POJ 1743 - Musical Theme 最长不重叠重复子串
题意: 给出一列数据,问你其中重复的最长连续子串的长度 但是有要求: 1. 长度至少为 5 . 2. 两串可以不相等,但两串每个对应位置的数字相减差值固定 (即 ...
随机推荐
- Altera FPGA AS,PS,Jtag配置模式区别
Altera FPGA AS,PS,Jtag配置模式区别 FPGA器件有三类配置下载方式:主动配置方式(AS)和被动配置方式(PS)和最常用的(JTAG)配置方式. AS模式(active seri ...
- Paper Reading - Show and Tell: A Neural Image Caption Generator ( CVPR 2015 )
Link of the Paper: https://arxiv.org/abs/1411.4555 Main Points: A generative model ( NIC, GoogLeNet ...
- JQuery 异步提交数据
类别添加 名称:   正在发送数据请求… <% dim strValue blnLogin = false; strValue = request.Form("t ...
- Echarts数据可视化全解
点击进入 Echarts数据可视化全解
- HDU 5233 Gunner II 离散化
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5233 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 微信小程序 功能函数 地图定位相对直线距离
GetDistance:function(lat1, lng1, lat2, lng2){ // console.log(lat1) var radLat1 = lat1 * Math.PI / ...
- C#开发移动应用 - 环境搭建
前言 其实从2013开始就想用Xamarin,奈何 当初收费一座大山压在身上 完全无法见得庐山真面目 后面2015,微软收购Xamarin,没过多久就宣布对个人用户免费..那个兴奋劲就别提了.. 兴奋 ...
- 【操作系统、UNIX环境编程】进程间通信
多个进程可以共享系统中的各种资源,但其中许多资源一次只能为一个进程使用,我们把一次仅允许一个进程使用的资源称为临界资源,许多物理设备都属于临界资源,如打印机等. Linux下进程间通信有如下几种方式: ...
- SQL优化套路
直奔主题: (1) SELECT(2) DISTINCT <SELECT_LIST>(3) FROM <LEFT_TABLE> <JOIN_TYPE> (4) JO ...
- 【.Net】Net开发
博客里的好多文章都是本人看着比较好,就转过来的,好少自己亲自去写点什么,也很少把自己学的一点心得于大家分享,今天特别想聊一下,关于本人做Net开发时的那段回忆! 一.关于知识的回忆 还记得Handle ...