poj1743 Musical Theme【后缀数组】【二分】
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 35044 | Accepted: 11628 |
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
题意:
在一个序列中找这样一个连续的子序列,他的长度大于等于5, 他在总序列中重复出现。【可以是加减一个定值之后的重复出现】。问这样的子序列的最长长度。
不重叠的重复子串。
思路:
首先由于他可以变调,所以其实我们找的是一个序列,序列中的相邻数之间的差值是一个定值。所以我们先把输入的序列预处理成相邻数之间的差值。
然后在求出SA和height。
总思路类似于https://www.cnblogs.com/wyboooo/p/9865584.html,二分答案,不同的是那道题可以重叠,这道题不行。
区别就在于求序列长度的不同。如果height[i]>=mid, 说明存在这样一个长度大于等于mid的串。但是他们可能是重叠的。
找到这个区间里后缀下标的最大最小值,相减就是答案的这个串,如果这个串大于k【没有等于,因为存的是相邻的差,比实际的个数要小1】,说明可行。
还要注意n=1的情况,RE了一次。
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 2e4 + ;
int n, k;
char str[maxn];
int s[maxn];
int sa[maxn];
int t1[maxn], t2[maxn], c[maxn];
int rnk[maxn], height[maxn]; void build_sa(int s[], int n, int m)
{
int i, j, p, *x = t1, *y = t2;
for(i = ; i < m; i++)c[i] = ;
for(i = ; i < n; i++)c[x[i] = s[i]]++;
for(i = ; i < m; i++)c[i] += c[i - ];
for(i = n - ; i >= ; i--)sa[--c[x[i]]] = i;
for(j = ; j <= n; j <<= ){
p = ;
for(i = n - j; i < n; i++)y[p++] = i;
for(i = ; i < n; i++)if(sa[i] >= j)y[p++] = sa[i] - j;
for(i = ; i < m; i++)c[i] = ;
for(i = ; i < n; i++)c[x[y[i]]]++;
for(i = ; i < m; i++)c[i] += c[i - ];
for(i = n - ; i >= ; i--)sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ;
x[sa[]] = ;
for(i = ; i < n; i++)
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + j] == y[sa[i] + j] ? p - :p++;
if(p >= n)break;
m = p;
}
} void get_height(int s[], int n)
{
int i, j, k = ;
//cout<<"SA:"<<endl;
for(i = ; i <= n; i++){
//cout<<sa[i]<<endl;
rnk[sa[i]] = i;
}
for(i = ; i <= n; i++){
if(k) k--;
j = sa[rnk[i] - ];
while(s[i + k] == s[j + k])k++;
height[rnk[i]] = k;
}
} bool check(int t)
{
int mmin = sa[], mmax = sa[];
for(int i = ; i <= n; i++){
if(height[i] < t){
mmin = mmax = sa[i];
}
else{
mmin = min(mmin, sa[i]);
mmax = max(mmax, sa[i]);
if(mmax - mmin > t)return true;
}
}
return false;
} int main()
{ while(scanf("%d", &n) != EOF && n){
for(int i = ; i <= n; i++){
scanf("%d", &s[i]);
}
for(int i = n; i >= ; i--){
s[i] = s[i] - s[i - ] + ;
}
for(int i = ; i < n; i++){
s[i] = s[i + ];
} if(n != )s[n] = ;
//n--;
build_sa(s, n + , );
get_height(s, n); int st = , ed = n, ans = -;
while(st <= ed){
int mid = (st + ed) / ;
//cout<<mid<<endl;
if(check(mid)){
st = mid + ;
ans = mid;
}
else{
ed = mid - ;
}
} if(ans < ){
printf("0\n");
}
else{
printf("%d\n", ans + );
} }
return ;
} /*
22
1 2 3 2 1 2 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/
poj1743 Musical Theme【后缀数组】【二分】的更多相关文章
- POJ1743 Musical Theme(后缀数组 二分)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 33462 Accepted: 11124 Description A m ...
- POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Tot ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)
洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- POJ1743 Musical Theme [后缀数组+分组/并查集]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
- POJ-1743 Musical Theme(后缀数组)
题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...
- poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)
题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...
- POJ 1743 Musical Theme ——后缀数组
[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...
随机推荐
- Oauth2.0(五):Authorization Code 授权
Authorization Code 方式适用于有自己的服务器的应用.之所以叫这个名字,是因为流程中引入了一个叫做 authorization code 的东西.这个东西是一个一次性的临时凭证,用来换 ...
- Spring中的类型转换与数据绑定(PropertyEditor、ConversionService、Data Binding、Formatter)
Spring早期使用PropertyEditor进行Object与String的转换.到Spring 3后,Spring提供了统一的ConversionService API和强类型的Converte ...
- 【Oracle】强制关闭会话
select sid, serial# from V$session where sid in (select sid from v$LOCK where TYPE in ('TM','TX')); ...
- 【剑指Offer学习】【面试题23:从上往下打印二叉树】
题目:从上往下打印出二叉树的每一个结点,同一层的结点依照从左向右的顺序打印. 二叉树结点的定义: public static class BinaryTreeNode { int value; Bin ...
- 如何下载腾讯视频的视频转为MP4常用格式视频
想起之前看过中央一台的<我要上春晚>中有个节目叫<迎春花>,两个女孩表现特别好,想下载这个视频,然后发现CCTV提供的客户端不好用,腾讯视频有,但是腾讯视频下载下来是qlv格式 ...
- VS中自定义代码段
如果数据属性的数量比较多,那么输入总是要花费较多的时间,这里有个小技巧,就是使用快捷的输入方法,但是VS自身提供的代码段是有限的,幸运的是我们可以通过:工具> 代码段管理器>添加来添加自定 ...
- Mysql主从热备
mysql主从配置比较不错的文章,大家可以参考下~ https://www.cnblogs.com/kissdodog/p/5422195.html
- FTP匿名访问修复方法
window2003 window2008
- Kubernetes 集群:规划与搭建
Kubernetes 集群环境: IP地址 主机名 角色 软硬件限制 192.168.119.134 master1 deploy ,master1 ,lb1 ,etcd (1) CPU至少1核,内存 ...
- Cesium添加水面
var viewer = new Cesium.Viewer('cesiumContainer');var waterPrimitive = new Cesium.Primitive({ //show ...