POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743
Musical Theme
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:
Transposed means that a constant positive or negative value is added to every note value in the theme subsequence. 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 30 Sample Output 5 Hint Use scanf instead of cin to reduce the read time.
Source |
题意概括:
给一个长度为 N 的序列,要求找长度不少于 5 的两个不重叠“相似”子串。
(本弱鸡一开始直接以为是找不重叠相同子串,样例都没过)。
相似的定义是长度相等且每一位的数字差都相等。
解题思路:
当然是传统经典口味:后缀数组啦(好吧,就是板子题)
首先处理出 sa 和 height(废话)(怎么处理?@模板)
当然主串就不是输入那个了, 而是相邻两个的值两两作差,得到一个新的主串,
在这个主串里找到两个不重叠相同子串,那么原序列里就对应两个相似主串了(为什么?因为题目要求的相似就是数字差相等嘛)
(不过要注意一点就是在新主串找的两个子串不能紧接在一起,因为这个串是数字差的结果,在原串中就会变成首尾相接了。)
二分可满足的长度 len, 判断是否有满足条件的两个不重叠子串。
判断过程: 先按 height 分组,然后比较组内的 最大的sa 和最小的sa 差值是否满足 len。
AC code:
//#include<bits/stdc++.h>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define mem(i, j) memset(i, j, sizeof(i))
#define inc(i, j, k) for(int i = j; i <= k; i++)
#define rep(i, j, k) for(int i = j; i < k; i++)
#define gcd(i, j) __gcd(i, j)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e5+;
int r[MAXN];
int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN];
int sa[MAXN]; int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *ws = tmp;
for (i = ; i < m; i++) ws[i] = ;
for (i = ; i < n; i++) ws[x[i] = r[i]]++;
for (i = ; i < m; i++) ws[i] += ws[i - ];
for (i = n - ; i >= ; i--) sa[--ws[x[i]]] = i;
for (j = , p = ; p < n; 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 < n; i++) wv[i] = x[y[i]];
for (i = ; i < m; i++) ws[i] = ;
for (i = ; i < n; i++) ws[wv[i]]++;
for (i = ; i < m; i++) ws[i] += ws[i - ];
for (i = n - ; i >= ; i--) sa[--ws[wv[i]]] = y[i];
for (swap(x, y), p = , x[sa[]] = , i = ; i < n; i++)
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
}
int Rank[MAXN]; //index range 0~n-1 value range 1~n
int height[MAXN]; //index from 1 (height[1] = 0)
void calheight(int *r, int *sa, int n)
{
int i, j, k = ;
for (i = ; i <= n; ++i) Rank[sa[i]] = i;
for (i = ; i < n; height[Rank[i++]] = k)
for (k ? k-- : , j = sa[Rank[i] - ]; r[i + k] == r[j + k]; ++k);
return;
}
int N, num[MAXN];
bool check(int len, int n)
{
int flag = false;
int mnn = n, mxx = -;
for(int i = ; i <= N; i++){ if((i == N && flag) || (height[i] < len && flag)){
flag = false;
mnn = min(mnn, sa[i-]);
mxx = max(mxx, sa[i-]);
if(mxx-mnn >= len){
return true;
}
mnn = n;
mxx = -;
}
else if(height[i] >= len){
flag = true;
mnn = min(mnn, sa[i-]);
mxx = max(mxx, sa[i-]);
}
}
return false;
} int main()
{
while(~scanf("%d", &N) && N != ){
inc(i, , (N-)) scanf("%d", &num[i]);
rep(i, , (N-)) r[i] = num[i+]-num[i]+;
r[N-] = ;
da(r, sa, N, );
calheight(r, sa, (N-));
// puts("zjj");
int ans = ;
int L = , R = N/, mid;
while(L <= R)
{
mid = (L+R)>>;
if(check(mid, N)){
L = mid+;
ans = max(ans, mid);
}
else R = mid-;
}
if(ans < ) puts("");
else printf("%d\n", ans+);
}
return ;
}
POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】的更多相关文章
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )
题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 : 根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
- poj 1743 Musical Theme (后缀数组+二分法)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16162 Accepted: 5577 De ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- [poj 1743] Musical Theme 后缀数组 or hash
Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...
- POJ 1743 Musical Theme ——后缀数组
[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...
- POJ 1743 Musical Theme 后缀数组 不可重叠最长反复子串
二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include & ...
- POJ.1743.Musical Theme(后缀数组 倍增 二分 / 后缀自动机)
题目链接 \(Description\) 给定一段数字序列(Ai∈[1,88]),求最长的两个子序列满足: 1.长度至少为5 2.一个子序列可以通过全部加或减同一个数来变成另一个子序列 3.两个子序列 ...
随机推荐
- nodejs添加jsonwebtoken验证
具体使用模块: 使用compression压缩处理请求响应.cors模块添加跨域.helmet安全模块.body-parser解析请求参数.jsonwebtoken用于生成及校验token.使用内置c ...
- Docker学习(一): 基本概念
特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! =============系列文章============= 1 ...
- 使用TreeDMS进行MySQL数据库的Web页面远程管理
在互联网应用蓬勃发展的时代背景下,各种各样的网络平台,网络应用,移动应用层出不穷,那么这些应用及平台都需要使用到数据库.如何高效的对数据进行日常维护.管理.监控成为迫切需要解决的问题. 基于web的方 ...
- 解决WCF跨机器调用时发生“调用方未由服务进行身份验证”的错误
1.服务器端Web.config配置文件,增加如下部分: <system.serviceModel> <bindings> <wsHttpBinding> < ...
- [ZJOI2008]杀蚂蚁 Solution
题目太长,不在此显示,见洛谷P2586 http://daniu.luogu.org/problem/show?pid=2586 模拟, 那就模拟呗: 各种WA, 然后好久才A了: 一种被社会报复了的 ...
- JavaWeb学习总结(五):HttpServletRespone对象(一)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- JavaWeb学习总结(十一):Session解决form表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
- Python3.7安装Geenlet
1.首先再python文件下的Scripts文件夹下有这几个文件: 2.打开Scripts文件夹下可能你会发现是空的,这时候就要先安装setuptools了,安装完后Script文件下就出现上图的文件 ...
- Java中int与Integer的区别
转自https://www.cnblogs.com/guodongdidi/p/6953217.html import java.lang.Integer; public class intDemo{ ...
- Android学习笔记(4)----Rendering Problems(The graphics preview in the layout editor may not be accurate)
在Android Studio中新建了一个 setting.xml 文件,布局好文件后,从 Text 界面切换到 Design 界面,显示了如下错误: 网上搜寻 The graphics previe ...