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. 【Spark调优】数据倾斜及排查

    [数据倾斜及调优概述] 大数据分布式计算中一个常见的棘手问题——数据倾斜: 在进行shuffle的时候,必须将各个节点上相同的key拉取到某个节点上的一个task来进行处理,比如按照key进行聚合或j ...

  2. new 操作符 做了什么

    new 操作符 做了什么 new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例. 假设Test是一个构造函数,通常在创建对象的实例时,要使用new,eg:test = new ...

  3. 序列化与反序列化之JSON

    在不同编程语言之间传递对象,须把对象序列化为标准格式,比如XML 但更好的方法是序列化为JSON,因为JSON表示出来就是一个字符串,可被所有语言读取,也可方便地存储到磁盘或者通过网络传输 JSON不 ...

  4. 使用SAX解析xml文档

    1.首先,在main方法中解析xml文档,具体代码如下: import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilder; import ...

  5. 纯 Html 5的 简单 TreeView 保存下思路以后有机会再完善。

    details p{margin-left:30px;}details.root details{margin-left:13px;} </style> <details class ...

  6. mybatis框架(1)---mybatis入门

    mybatis入门   MyBatis是什么? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了goog ...

  7. select实现高并发服务器

    前言:周末学了两天网络编程,把之前的不懂一些问题基本掌握了,例如TCP状态转换图.close和shutdown函数的区别.select函数等,今天分享给大家. 一.网络编程基础知识 在写代码之前,需要 ...

  8. Future、Callable 、FutureTask详解

    1.Future和Callable Future是一个接口表示异步计算的结果,它提供了检查计算是否完成的方法,以等待计算的完成,并获取计算的结果.Future提供了get().cancel().isC ...

  9. 【EF6学习笔记】(十二)EF高级应用场景

    本篇原文链接:Advanced Entity Framework Scenarios 本篇主要讲一些使用Code First建立ASP.NET WEB应用的时候除了基础的方式以外的一些扩展方式方法: ...

  10. MFC原理第四讲.动态创建机制

    MFC原理第四讲.动态创建机制 一丶要学习的知识点以及简介 动态创建是什么意思? 动态创建其实就是跟C++的new一样.都是创建对象.但是规避了C++语法的缺陷. 例如: char * ClassNa ...