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

 
 
正解:后缀数组
解题报告:
  首先声明,这道题是一道模板题(水题)。
  罗穗骞的论文的例题。中午的时候,我满心欢喜的打算下午要切掉至少5道后缀数组题,然后被这道题卡了一个下午加一个晚上。
  思想确实简单,但我就是无限wa,迷之wa。。。几个小时之后,各种努力之后,最后发现,特判的时候没有读数。。。
  多么痛的领悟。半天时间就这么报废了。。。我很不开心,不讲题解了,直接看代码吧。
 
 //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的更多相关文章

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

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

  2. POJ1743 Musical Theme [后缀数组]

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

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

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

  4. poj1743 Musical Theme【后缀数组】【二分】

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 35044   Accepted: 11628 D ...

  5. POJ-1743 Musical Theme,后缀数组+二分!

                                                        Musical Theme 人生第一道后缀数组的题,采用大众化思想姿势极其猥琐. 题意:给你n个 ...

  6. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

  7. POJ1743 Musical Theme (后缀数组 & 后缀自动机)最大不重叠相似子串

    A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...

  8. POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)

    A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...

  9. 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变化都一样的 思路: ...

随机推荐

  1. java 21 - 3 字符输入流

    所谓的输入流,就是读取文件中的数据 同样的,字符输入流InputStreamReader 4个构造方法,不过2个比较常用: 构造方法: A:InputStreamReader(InputStream ...

  2. 将TP引擎改为smarty引擎

    在common/config文件里设置'TMPL_ENGINE_TYPE'=>'Smarty'即可,但要注意,在模板文件里的css样式{}要用一对{literal}{/literal}标签包裹, ...

  3. Go Walk教程 - 流程控制( switch)

    Go的 switch 非常灵活,表达式不必是常量或整数,执行的过程从上至下,直到找到匹配项,不要break: var score =98 var result string switch score/ ...

  4. 未能解析此远程名称:'nuget.org' 的解决方法

    今天用Nuget下一个程序包时,发现Nuget挂了: 未能解析此远程名称:'nuget.org' . 浏览器打开  http://nuget.org  失败. 使用cmd命令 输入nslookup n ...

  5. C语言 三级指针的应用

    //三级指针的使用 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #includ ...

  6. angularjs中只显示选中的radio的值

    angularjs中,只显示选中的radio的值.主要是相同的radio,name属性值要相同还有ng-model的值要相同,同时要指定value值.这样选中的时候就会在下面的div中显示选中的值了. ...

  7. 深入理解abstract class和interface(转)

    原文地址 深入理解abstract class和interface java提高篇(四)-----抽象类与接口

  8. [CareerCup] 11.8 The Rank of Number 数的排行

    11.8 Imagine you are reading in a stream of integers. Periodically, you wish to be able to look up t ...

  9. 20145222黄亚奇《Java程序设计》第1周学习总结

    教材学习内容总结 BJVM是Java程序唯一认识的操作系统,其可执行文件为.class文档 Java的三大平台为Java SE,Java EE,Java ME. Java SE的四个部分为:JVM,J ...

  10. Pjax.js防刷新技术

    自我感觉良好,所以拿出现在自己用的 Pjax.js 分享给大家 当然 这个版本是 经过本人修改后的版本,跟其它 拿过来就用的 不一样 而且区别还不小 大多的 Pjax 都是 跟后台无关的,而这个版本是 ...