POJ1743 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
Source
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
int ch[MAXN];
int n,m;
int wa[MAXN],wb[MAXN],c[MAXN];
int rank[MAXN],height[MAXN];
int sa[MAXN];
int ans; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void da(int m,int n){
int i,*x=wa,*y=wb;
for(i=;i<=m;i++) c[i]=;
for(i=;i<=n;i++) c[x[i]=ch[i]]++;
for(i=;i<=m;i++) c[i]+=c[i-];
for(i=n;i>=;i--) sa[c[x[i]]--]=i;
for(int k=,p;k<=n;k=k*) {
p=;
for(i=n-k+;i<=n;i++) y[++p]=i;
for(i=;i<=n;i++) if(sa[i]>k) y[++p]=sa[i]-k;
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); x[sa[]]=; p=;
for(i=;i<=n;i++) x[sa[i]]=(y[sa[i-]]==y[sa[i]] && y[sa[i-]+k]==y[sa[i]+k])?p:++p;
if(p==n) break; m=p;
}
} inline void calheight(){
int i,j,k=;
for(i=;i<=n;i++) {
if(k) k--;
j=sa[rank[i]-];
while(ch[i+k]==ch[j+k]) k++;
height[rank[i]]=k;//是rank[i]!!!
}
} inline bool check(int x){
int i=,minl,maxl;
while() {
while(i<=n && height[i]<x) i++;
if(i>n) break;
minl=maxl=sa[i-];
while(i<=n && height[i]>=x) minl=min(minl,sa[i]),maxl=max(maxl,sa[i]),i++;
if(maxl-minl>x) return true;//不能等号!!!记录的是差
}
return false;
} inline void work(){
while() {
n=getint(); if(n==) break;
memset(ch,,sizeof(ch)); memset(sa,,sizeof(sa));
memset(wa,,sizeof(wa)); memset(wb,,sizeof(wb));
memset(height,,sizeof(height)); memset(rank,,sizeof(rank));
for(int i=;i<=n;i++) ch[i]=getint();
if(n<) { printf("0\n"); continue; }
for(int i=;i<n;i++) ch[i]=ch[i+]-ch[i],ch[i]+=;
ch[n]=;
da(,n);
for(int i=;i<=n;i++) rank[sa[i]]=i;
calheight();
int l=,r=n,mid;
while(l<r) {
mid=(l+r)/;
if(check(mid)) l=mid+;
else r=mid;
}
if(l<=) printf("0\n");
else printf("%d\n",l);
}
} int main()
{
work();
return ;
}
POJ1743 Musical Theme的更多相关文章
- POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Tot ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- POJ1743 Musical Theme [后缀数组+分组/并查集]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- poj1743 Musical Theme【后缀数组】【二分】
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 35044 Accepted: 11628 D ...
- POJ-1743 Musical Theme,后缀数组+二分!
Musical Theme 人生第一道后缀数组的题,采用大众化思想姿势极其猥琐. 题意:给你n个 ...
- POJ1743 Musical Theme(后缀数组 二分)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 33462 Accepted: 11124 Description A m ...
- POJ1743 Musical Theme (后缀数组 & 后缀自动机)最大不重叠相似子串
A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...
- POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)
A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...
- POJ1743 Musical Theme 最长重复子串 利用后缀数组
POJ1743 题目意思是求不重叠的最长相同变化的子串,输出该长度 比如1 2 3 4 5 6 7 8 9 10,最长长度为5,因为子串1 2 3 4 5 和 6 7 8 9 10变化都一样的 思路: ...
随机推荐
- 第20章 DLL高级技术(2)
20.3 延迟载入DLL 20.3.1延迟载入的目的 (1)如果应用程序使用了多个DLL,那么它的初始化可能比慢,因为加载程序要将所有必需的DLL映射到进程的地址空间.→利用延迟加载可将载入过程延伸到 ...
- Linux基于libmemcached,php扩展memcached的安装
安装环境:CentOS 6.4 php的扩展memcache,不支持cas,所以我们要装memcached扩展,memcached扩展是基于libmemcached,所以要先安装libmemcache ...
- 判断一个值是否在数组里,可以检测数字,字符串,json对象
Array.prototype.indexOf = function (val) {//判断数组是否存在某个值,如果存在返回该值对应的索引,否则返回-1 for (var i = 0; i < ...
- 【MFC】无边框窗体 WS_THICKFRAME
1.没有WS_THICKFRAME,有WS_SYSMENU,就没办法通过相应WM_NCHITTEST,去处理窗口的缩放.2.没有WS_THICKFRAME,也没有WS_SYSMEN,在xp系统下任务栏 ...
- Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- JS 之继承
ECMAScript继承是通过原型链来继承的.基本思想是利用原型来让一个引用类型继承另一个引用类型的属性和方法,使原型变为另一个对象的实例.通过原型链实现继承时,不能使用对象字面量创建原型方法,避免重 ...
- Caffe学习系列(21):caffe图形化操作工具digits的安装与运行
经过前面一系列的学习,我们基本上学会了如何在linux下运行caffe程序,也学会了如何用python接口进行数据及参数的可视化. 如果还没有学会的,请自行细细阅读: caffe学习系列:http:/ ...
- 一个基于.NET平台的自动化/压力测试系统设计简述
AutoTest系统设计概述 AutoTest是一个基于.NET平台实现的自动化/压力测试的系统,可独立运行于windows平台下,支持分布式部署,不需要其他配置或编译器的支持.(本质是一个基于协议的 ...
- flatbuffers 使用问题记录
1. 命名空间的问题 ----------------------------- namespace 1.0.3 版本包含文件类型前面不需要加命名空间,但是1.1.0 中包含需要在类型前加命名空间 i ...
- CSS元素分类
快级元素:在html中<div>,<p>,<h1>,<form>,<ul>,<li>就是块级元素. ...