POJ 1743 Musical Theme(后缀数组 + 二分)题解
题意:一行数字,定义如下情况为好串:
1.连续一串数字,长度大于等于5
2.这行数字中多次出现这串数字的相似串,相似串为该串所有数字同加同减一个数字,如 1 2 3 和 5 6 7
3.至少有一个相似串和他不相交
问最长多少
思路:先作差,那么不相交相似问题就转化为不相交相同子串问题。我们二分子串长度,然后O(n)求解是否可行。求解过程:作差之后,相同子串长度为len那么原串相似长度为len + 1,这个自己去举例子就懂了。所以我们每次二分出一个len,每次找一个区间,这个区间中height >= len - 1,然后找到这个区间的最左和最右起点,算出这两个的距离是否能放进一个len长度不相交。
代码:
#include<cmath>
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e4 + ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int str[maxn], num[maxn];
int t1[maxn], t2[maxn], c[maxn];
int rk[maxn], height[maxn], sa[maxn];
bool cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(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++) rk[sa[i]] = i;
for(i = ; i < n; i++){
if(k) k--;
j = sa[rk[i] - ];
while(str[i + k] == str[j + k]) k++;
height[rk[i]] = k;
}
}
int n;
bool solve(int len){
int l = INF, r = -INF;
for(int i = ; i <= n; i++){
if(height[i] >= len - ){
l = min(l, min(sa[i],sa[i - ]));
r = max(r, max(sa[i], sa[i - ]));
if(r - l >= len) return true;
}
else{
l = INF, r = -INF;
}
}
return false;
} int main(){
while(~scanf("%d", &n) && n){
for(int i = ; i < n; i++)
scanf("%d", &num[i]);
n--;
for(int i = ; i < n; i++)
str[i] = num[i + ] - num[i] + ;
da(n, + );
int l = , r = n, ans = ;
while(l <= r){ //枚举长度
int m =(l + r) >> ;
if(solve(m)){
ans = m;
l = m + ;
}
else{
r = m - ;
}
}
if(ans >= ) printf("%d\n", ans);
else printf("0\n");
}
return ;
}
POJ 1743 Musical Theme(后缀数组 + 二分)题解的更多相关文章
- 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: 28435 Accepted: 9604 Descri ...
- POJ 1743 Musical Theme ——后缀数组
[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- [poj 1743] Musical Theme 后缀数组 or hash
Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...
- poj 1743 Musical Theme (后缀数组+二分法)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16162 Accepted: 5577 De ...
- POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )
题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 : 根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...
- POJ.1743.Musical Theme(后缀数组 倍增 二分 / 后缀自动机)
题目链接 \(Description\) 给定一段数字序列(Ai∈[1,88]),求最长的两个子序列满足: 1.长度至少为5 2.一个子序列可以通过全部加或减同一个数来变成另一个子序列 3.两个子序列 ...
- POJ 1743 Musical Theme 后缀数组 不可重叠最长反复子串
二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include & ...
- POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)
洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...
随机推荐
- vue利用vue ui命令创建项目
上次用git bash,用create 命令创建vue项目,这是玩个炫酷的------vue ui (前提是有安装node.js). 在目标文件 vue ui 可以看到他在8000端口出现了一个gu ...
- pandas读取文件报错
path = 'D:/Postgraduate/Python/Machine Learning/小象学院机器学习/08.回归实践/8.Regression代码/8.Regression/8.Adver ...
- .NET Core 2.1 源码学习:看 SocketsHttpHandler 如何在异步方法中连接 Socket
在 .NET Core 2.1 中,System.Net.Sockets 的性能有了很大的提升,最好的证明是 Kestrel 与 HttpClient 都改为使用 System.Net.Sockets ...
- hdu 1045
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) Me ...
- Codeforces 1062 - A/B/C/D/E - (Undone)
链接:http://codeforces.com/contest/1062 A - Prank - [二分] 题意: 给出长度为 $n(1 \le n \le 100)$ 的数组 $a[1 \sim ...
- 架构3(基于LVS LB集群解决方案一:piranha)
1.实现调度器的HA 2.对realserver做健康检测 3.动态维护IPVS路由表 pulse 活跃和备用lvs路由器中都会运行pulse守护进程,在备用路由器中,pulse向活跃的服务器的公共接 ...
- Redis的数据结构之哈希
存储Hash String key和String Value的Map容器 每一个Hash可以存储4294967295个键值对 存储Hash常用命令: 赋值 取值 删除 增加数字 判断字段是否存在 获取 ...
- docker容器与镜像
就像cad图层概念 数据卷就是为了完成数据持久化操作
- sap 软件架构
1:
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...