POJ1743 Musical Theme

时间:2022-10-29 21:01:52

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 &lbrack;后缀数组&rsqb;

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

  3. POJ1743 Musical Theme &lbrack;后缀数组&plus;分组&sol;并查集&rsqb;

    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,后缀数组&plus;二分!

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

  6. POJ1743 Musical Theme&lpar;后缀数组 二分&rpar;

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

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

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

  8. POJ-1743 Musical Theme&lpar;最长不可重叠子串,后缀数组&plus;二分&rpar;

    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. sac 文档使用

    目前我遇到的问题是我想要得到BHE,BHN 方向的数据,但发现IRIS下载的数据都是BH1,BH2 方向的,很困惑,请教大神后发现,原来IRIS之所以提供BH1,BH2方向是因为很多时候台站的水平方向 ...

  2. MVC开发经验总结

    网站开发中技术问题的笔记 以下经验基于 MVC 5.2.3 Javascript BUG思考 200 OK真的表示没有问题吗? 有时候Status 200作为WebAPI的返回值,并不表示真的OK,例 ...

  3. 简单两句话解释下prototype和&lowbar;&lowbar;proto&lowbar;&lowbar;

    先上两句代码: var Person = function () {}; var p = new Person(); 把new的过程拆分成以下三步: <1> var p={}; 也就是说, ...

  4. 参加 TiD 2015 是怎样一番体验?

    人生有很多第一次,(专程打飞的去帝都)参加软件大会,我也是第一次.   TiD   说到软件大会,QCon.PyCon 什么的早已如雷贯耳,没吃过猪肉还没见过猪跑?但对于 TiD,确实还是头一次听说. ...

  5. 判断浏览器是否为IE内核的最简单的方法

    没啥说的,直接贴代码,算是ie hack了. if (!+[1,]) { alert('is ie'); }

  6. android开发教程(八)——环境搭建之java-ndk

    目录 android ndk是android用于开发本地代码的开发工具包.它提供C/C++交叉编译工具.android内核.驱动.已有的C/C++代码,都需要ndk来支持开发. 目前支持以下平台:ar ...

  7. SugarSync网盘之XML解析

    iOS的XML解析 刚在应用里支持了SugarSync网盘.其实也是第一次听说这个网盘,不过在国外貌似还蛮有名,这些都不是重点,重点是借此来总结一下iOS的XML解析.Xml想必也不陌生了,但是在iO ...

  8. python脚本,计算起点终点高程

    import arcpy >>> import arcpy ... gd="D:/项目/shp/Pipe.gdb/ZK/GDPOINT" ... gx=&quot ...

  9. Django 查询集简述

    通过模型中的管理器构造一个查询集(QuerySet),来从数据库中获取对象.查询集表示从数据库中取出来的对象的集合.它可以含有零个.一个或者多个过滤器.过滤器基于所给的参数限制查询的结果. 从SQL ...

  10. 如何让移植的嵌入式ARM显示中文汉字

    如果你急于在ARM开发板上看到Qt显示中文,而不介意稍次的效果,可以在运行Qt程序时,增加设置字体的参数,比如运行名为hello的Qt程序:./hello -fn unifont 1.首先,需要文泉驿 ...