POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-1743
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 32402 | Accepted: 10808 |
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
题意:
给出一串数字,定义theme为:长度不小于5,从左到右以相同的变化规律出现了不止一次,并且不能重叠。求最长的theme。实际上是求:字符串的重复出现且不重叠的最长子串。
题解:
1.由于求的是变化规律,所以要求出相邻两个数的差值,得到新的一串数字。然后求出新串的后缀数组。
2.二分答案,即“重复出现且不重叠的最长子串”的长度k。然后根据是否存在这样的子串来缩小k的范围,最终得到答案。那么怎样判断是否存在“重复出现且不重叠的长度为k的子串”呢?
2.1 把后缀按名次排成一列,如果前m个后缀(第一名除外)与它的前一名的最长公共前缀都大于等于k(二分时的mid),即height[2~m]>=k,则可以说明这m个后缀的最长公共前缀大于等于k。所以可以得出结论:k把所有后缀分成若干组,并且每一组的最长公共前缀大于等于k(可以单独一个后缀作为一组)。那么,我们只需要判断:是否存在一组后缀,使得max(sa[i]) - min(sa[i]) >= k。
2.2 视图更加直观:
2.3 参考:http://blog.csdn.net/huangzhengdoc/article/details/53573198
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = +; bool cmp(int *r, int a, int b, int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
} int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
int t1[MAXN], t2[MAXN], c[MAXN];
void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[i] = str[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]] = cmp(y, sa[i-], sa[i], j)?p-:p++;
if(p>=n) break;
m = p;
} int k = ;
n--;
for(i = ; i<=n; i++) Rank[sa[i]] = i;
for(i = ; i<n; i++)
{
if(k) k--;
j = sa[Rank[i]-];
while(str[i+k]==str[j+k]) k++;
height[Rank[i]] = k;
}
} bool test(int mid, int n)
{
int minn = sa[], maxx = sa[];
for(int i = ; i<=n; i++)
{
if(height[i]<mid)
minn = maxx = sa[i];
else
{
maxx = max(maxx, sa[i]);
minn = min(minn, sa[i]);
if(maxx-minn>=mid)
return true;
}
}
return false;
} int main()
{
int n;
while(scanf("%d",&n)&&n)
{
for(int i = ; i<n; i++) scanf("%d", &r[i]);
for(int i = ; i<n-; i++) r[i] = r[i+]-r[i]+;
r[--n] = ;
DA(r, sa, Rank, height, n, );
int l = , r = n/;
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid, n))
l = mid + ;
else
r = mid - ;
}
if(r<) printf("0\n");
else printf("%d\n", r+);
}
}
POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串的更多相关文章
- 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 ...
- poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)
题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...
- POJ1743 Musical Theme(后缀数组 二分)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 33462 Accepted: 11124 Description A m ...
- POJ-1743 Musical Theme(后缀数组)
题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...
- [Poj1743] [后缀数组论文例题] Musical Theme [后缀数组不可重叠最长重复子串]
利用后缀数组,先对读入整数处理str[i]=str[i+1]-str[i]+90这样可以避免负数,计算Height数组,二分答案,如果某处H<lim则将H数组分开,最终分成若干块,判断每块中是否 ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- poj 1743 Musical Theme (后缀数组+二分法)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16162 Accepted: 5577 De ...
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
随机推荐
- linux中at命令
名称 : linux at命令 使用权限 : 所有使用者 使用方式 : at -V [-q queue] [-f file] [-mldbv] TIME 说明 : linux at命令可以让使用者指定 ...
- 学习日记之享元模式和Effective C++
享元模式(Flyweight):运用共享技术有效地支持大量细粒度的对象. (1),享元模式能够避免大量很相似的开销.在程序设计中,有时须要生成大量细粒度的类实例来表示数据.假设能发现这些实例除了几个參 ...
- 2017.04.20 Adams仿真介绍
Adams 仿真 | 验证"隐性机器人模型"概念,提高视觉伺服精度 产品:Adams行业:科研优势: 1.Adams 仿真可精确预测机器人的位置和方位 2.仿真在理论工作验证中起着 ...
- struts2注解总结----@Action和@Result
除了使用配置文件配置之外,还能够使用注解来配置 以下是一些经常使用的注解 介绍: @Action/@Actions: @Action指定一个类为action,相应配置文件里的<action> ...
- html5,audio音乐播放器
最终,做了自己原来一直想要实现的事儿.得出的结果是,有些事儿一旦開始做了,那么它就并非非常难. 如今的我,正听着自己的播放器放出的<光辉岁月>写这篇周六清晨的博文.写的不是非常好.但也请各 ...
- android:scrollbar的一些属性
1. activity_maim.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Netty(三):线程模型
Netty中支持单线程模型,多线程模型,主从多线程模型. 1 单线程模型 在ServerBootstrap调用方法group的时候,传递的参数是同一个线程组,且在构造线程组的时候,构造参数为1,这种开 ...
- cmake学习之-project
一.系统版本 cmake version: 3.5.2 系统版本: Ubuntun 16.04 cmake docment: 3.14.4 最后更新: 2019-05-31 二.指令说明 projec ...
- 数据挖掘之pandas
sdata={'语文':89,'数学':96,'音乐':39,'英语':78,'化学':88} #字典向Series转化 @@ >>> studata=Series(sdata) & ...
- Multicast注册中心
1 2 3 4 提供方启动时广播自己的地址. 消费方启动时广播订阅请求. 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了unicast=false,则广播给订阅者. 消费方收到提供方地址时, ...