POJ 1743 Musical Theme 二分+后缀数组
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
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- #pragma comment(linker, "/STACK:102400000,102400000")
- #define ls i<<1
- #define rs ls | 1
- #define mid ((ll+rr)>>1)
- #define pii pair<int,int>
- #define MP make_pair
- typedef long long LL;
- const long long INF = 1e18+1LL;
- const double Pi = acos(-1.0);
- const int N = 2e5+, M = 2e5+, mod = 1e9+, inf = 2e9;
- ///heght[i] 表示 Suffix(sa[i-1])和Suffix(sa[i]) 的最长公共前缀:
- ///rank[i] 表示 开头为i的后缀的等级:
- ///sa[i] 表示 排名为i的后缀 的开头位置:
- int *rank,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
- bool cmp(int *r,int a,int b,int l) {
- return r[a] == r[b] && r[a+l] == r[b+l];
- }
- void SA(int *r,int *sa,int n,int m) {
- int *x=wa,*y=wb,*t;
- for(int i=;i<m;++i)wm[i]=;
- for(int i=;i<n;++i)wm[x[i]=r[i]]++;
- for(int i=;i<m;++i)wm[i]+=wm[i-];
- for(int i=n-;i>=;--i)sa[--wm[x[i]]]=i;
- for(int i=,j=,p=;p<n;j=j*,m=p){
- for(p=,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)wm[i]=;
- for(i=;i<n;++i)wm[x[y[i]]]++;
- for(i=;i<m;++i)wm[i]+=wm[i-];
- for(i=n-;i>=;--i)sa[--wm[x[y[i]]]]=y[i];
- for(t=x,x=y,y=t,i=p=,x[sa[]]=;i<n;++i) {
- x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
- }
- }
- rank=x;
- }
- void Height(int *r,int *sa,int n) {
- for(int i=,j=,k=;i<n;height[rank[i++]]=k)
- for(k?--k:,j=sa[rank[i]-];r[i+k] == r[j+k];++k);
- }
- int n,a[N];
- int check(int len) {
- int i = , mx, mi;
- while() {
- while(i <= n && height[i] < len) i++;
- if(i > n) break;
- mx = sa[i-];
- mi = sa[i-];
- while(i <= n && height[i] >= len) {
- mx = max(mx,sa[i]);
- mi = min(mi,sa[i]);
- i++;
- }
- if(mx - mi >= len) return ;
- }
- return ;
- }
- int main() {
- while(~scanf("%d",&n)) {
- if(!n) break;
- a[] = ;
- for(int i = ; i < n; ++i) scanf("%d",&a[i]);
- n--;
- for(int i = ; i < n; ++i) r[i] = a[i+]-a[i] + ;
- r[n] = ;
- SA(r,sa,n+,);
- Height(r,sa,n);
- int ll = , rr = n,ans = ;
- while(ll <= rr) {
- int md = (ll + rr) >> ;
- int bo = check(md);
- if(bo) ans = md,ll = md + ;
- else rr = md - ;
- }
- if(ans >= ) {
- printf("%d\n",ans+);
- } else puts("");
- }
- return ;
- }
POJ 1743 Musical Theme 二分+后缀数组的更多相关文章
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)
永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...
- POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
- POJ 1743 Musical Theme(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1743 [题目大意] 给出一首曲子的曲谱,上面的音符用不大于88的数字表示, 现在请你确定它主旋律的长度,主旋律指的是出现超过一次, ...
- POJ - 1743 Musical Theme (后缀数组)
题目链接:POJ - 1743 (不可重叠最长子串) 题意:有N(1<=N<=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的子串,它需要 ...
- Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22499 Accepted: 7679 De ...
- POJ 1743 Musical Theme(后缀数组)
题意:有n个数值,算出相邻两个值的差值,此时有n-1个值的序列,把这序列当做字符串的话,求最长重复子串,且这两个子串不能重叠. 分析:后缀数组解决.先二分答案,把题目变成判定性问题:判断是否存在两个长 ...
- POJ 1743 Musical Theme(后缀数组 + 二分)题解
题意:一行数字,定义如下情况为好串: 1.连续一串数字,长度大于等于5 2.这行数字中多次出现这串数字的相似串,相似串为该串所有数字同加同减一个数字,如 1 2 3 和 5 6 7 3.至少有一个相似 ...
- poj 1743 Musical Theme【后缀自动机】
不是很神的一道题,一般. 先差分,最后答案需要+1. 一个right集的len即为该right集的最长相同后缀,考虑到不能重复,所以处理一下该right集的最大与最小的ri,最后答案ans=max(a ...
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
随机推荐
- npm全局安装和本地安装和本地开发安装(npm install --g/--save/--save-dev)
详细说明参考:http://www.cnblogs.com/PeunZhang/p/5629329.html 我个人理解: 1.全局安装(npm install -g)是为了用命令行,比如在windo ...
- Zabbix监控mysql主从复制状态
原理 mysql slave show slave status\G 在输出信息中查看I/O线程和SQL线程的状态值(YES为正常,NO为错误) Slave_IO_Running: Yes Slave ...
- 搭建haproxy
1:下载haproxy wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz2:解压,编译,安装 tar zxf hapr ...
- mate标签
<meta charset='utf-8'> <!-- 优先使用 IE 最新版本和 Chrome --> <meta http-equiv="X-UA-C ...
- jquery实现简单瀑布流布局(续):图片懒加载
# jquery实现简单瀑布流布局(续):图片懒加载 这篇文章是jquery实现简单瀑布流布局思想的小小扩展.代码基于前作的代码继续完善. 图片懒加载就是符合某些条件时才触发图片的加载.最常见的具体表 ...
- 【06-18】CentOS使用笔记
使用中文输入法 搜狗输入法只支持Ubuntu sudo yum install "@Chinese Support" [系统]--->[首选项]--->[输入法]--& ...
- Pandas-多表操作
Pandas包对多个数据表(DataFrame)的常用整合功能. 目录 merge join concat append combin_first merge 合并 pandas.merge可根据一个 ...
- Linux入门50指令
1, mkdir创建目录 make directory 可以带着路径,在什么位置创建什么目录 如:mkdir /etc/date 在目录etc下创建date目录 重要参数 –p 递归创建,mkdir ...
- 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?
file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...
- bash 语法使用
1.定义函数时,不需要使用function作为函数的命名. 函数不需要形参. 函数名不能以数字作为开头 main() { in ) 1_start ;; ) 1_start 5_start ;; ) ...