Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 33462   Accepted: 11124

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:

  • 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 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
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

Use scanf instead of cin to reduce the read time.

Source

题目大意:

给出一个长度为$n$的序列,让你找出最长的相似子串。这里相似定义为两个串每次字符对应的差值相同

Sol:
很显然,我们可以对原序列进行差分,这样如果在原序列中长度为$n$的互不相交的相同的字符串,那么答案为$n + 1$

这是一个经典的问题。首先二分答案,然后对$height$数组分组,若$sa[i] - sa[j] > ans$那么可以更新答案

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > ''){if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N;
int s[MAXN], sa[MAXN], rak[MAXN], tp[MAXN], tax[MAXN], height[MAXN], P, M;
void Qsort() {
for(int i = ; i <= M; i++) tax[i] = ;
for(int i = ; i <= N; i++) tax[rak[i]]++;
for(int i = ; i <= M; i++) tax[i] += tax[i - ];
for(int i = N; i >= ; i--) sa[ tax[rak[tp[i]]]-- ] = tp[i];
}
void SuffixSort() {
M = ;
for(int i = ; i <= N; i++) rak[i] = s[i], tp[i] = i;
Qsort();
for(int w = , p = ; p < N; M = p, w <<= ) {
p = ;
for(int i = ; i <= w; i++) tp[++p] = N - i + ;
for(int i = ; i <= N; i++) if(sa[i] > w) tp[++p] = sa[i] - w;
Qsort(); swap(tp, rak);
rak[sa[]] = p = ;
for(int i = ; i <= N; i++)
rak[sa[i]] = (tp[sa[i]] == tp[sa[i - ]] && tp[sa[i] + w] == tp[sa[i - ] + w]) ? p : ++p; }
int j = , k = ;
for(int i = ; i <= N; i++) {
if(k) k--;
int j = sa[rak[i] - ];
while(s[i + k] == s[j + k]) k++;
height[rak[i]] = k;
}
//for(int i = 1; i <= N; i++) printf("%d ", sa[i]); puts("");
}
bool check(int len) {
int mx = sa[], mi = sa[];
for(int i = ; i <= N; i++) {
if(height[i] >= len - )
mx = max(sa[i], mx),
mi = min(sa[i], mi);
else
mx = mi = sa[i];
if(mx - mi >= len) return ;
}
return ;
}
int solve() {
int l = , r = N, ans = ;
while(l <= r) {
int mid = l + r >> ;
if(check(mid)) l = mid + , ans = mid;
else r = mid - ;
}
return ans;
}
int main() {
while(scanf("%d", &N) && N != ) {
for(int i = ; i <= N; i++) s[i] = read();
for(int i = N; i >= ; i--) s[i] -= s[i - ] - ;
SuffixSort();
int ans = solve();
if(ans >= )
printf("%d\n", ans);
else
printf("%d\n", );
} return ;
}
/*
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
*/

POJ1743 Musical Theme(后缀数组 二分)的更多相关文章

  1. POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Tot ...

  2. Poj 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...

  3. POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)

    洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...

  4. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  5. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  6. Poj 1743 Musical Theme (后缀数组+二分)

    题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...

  7. POJ-1743 Musical Theme(后缀数组)

    题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...

  8. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

  9. POJ 1743 Musical Theme ——后缀数组

    [题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...

随机推荐

  1. C 线性表的顺序存储实现及插入、删除等操作示例

    一.线性表的定义 线性表(Linear List)是由同一类型元素构成的有序序列的线性结构.线性表中元素的个数称为线性表的长度:线性表内没有元素(长度为0)时,称为空表:表的起始位置称为表头,表的结束 ...

  2. WPF中TreeView控件的使用案例

    WPF总体来说还是比较方便的,其中变化最大的主要是Listview和Treeview控件,而且TreeView似乎在WPF是一个备受指责的控件,很多人说他不好用.我这个demo主要是在wpf中使用Tr ...

  3. Kali学习笔记39:SQL手工注入(1)

    终于到了SQL注入 最大的.最经典的.最常见的Web漏洞就是SQL注入漏洞 SQL注入的原理这里就不说了,百度 打开DVWA,SQL注入测试模块 测试单引号,发现出错,于是想到测试语句: 1' or ...

  4. python中numpy.ndarray.shape的用法

    今天用到了shape,就顺便学习一下,这个shape的作用就是要把矩阵进行行列转换,请看下面的几个例子就明白了: >>> import numpy as np >>> ...

  5. 8.python面向对象编程

    基本概念 Class 类一个类即是对一类拥有相同属性的对象的抽象.蓝图.原型.在类中定义了这些对象的都具备的属性(variables(data)).共同的方法 Object 对象 一个对象即是一个类的 ...

  6. sql server 索引阐述系列一索引概述

    一. 索引概述 关于介绍索引,有一种“文章太守,挥毫万字,一饮千钟”的豪迈感觉,因为索引需要讲的知识点太多.在每个关系型数据库里都会作为重点介绍,因为索引关系着数据库的整体性能, 它在数据库性能优化里 ...

  7. Android--UI之Fragment

    前言 开门见山开篇名义,本篇博客将讲解一下Android中Fragment的内容,必要的地方会提供相应的演示代码,并且会在最后给出源码下载. 本文主要有以下内容: 什么是Fragment 如何创建一个 ...

  8. 【原创】驱动加载之CreateService

    SC_HANDLE WINAPI CreateService( _In_ SC_HANDLE hSCManager, _In_ LPCTSTR lpServiceName, _In_opt_ LPCT ...

  9. 【ASP.NET MVC系列】浅谈ASP.NET 页面之间传值的几种方式

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  10. 翻译:INSERT(已提交到MariaDB官方手册)

    本文为mariadb官方手册:INSERT的译文. 原文:https://mariadb.com/kb/en/insert/我提交到MariaDB官方手册的译文:https://mariadb.com ...