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

LouTiancheng@POJ

求最长不可重叠子串。能够后缀数组+二分解决

先把输入的数字前后两两做差,然后建立后缀数组。二分就可以

/*************************************************************************
> File Name: POJ1743.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月31日 星期二 15时43分29秒
************************************************************************/ #include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector> using namespace std; const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL; class SuffixArray
{
public:
static const int N = 20010;
int init[N];
int X[N];
int Y[N];
int Rank[N];
int sa[N];
int height[N];
int buc[N];
int size; void clear()
{
size = 0;
} void insert(int n)
{
init[size++] = n;
} bool cmp(int *r, int a, int b, int l)
{
return (r[a] == r[b] && r[a + l] == r[b + l]);
} void getsa(int m = 256)
{
init[size] = 0;
int l, p, *x = X, *y = Y, n = size + 1;
for (int i = 0; i < m; ++i)
{
buc[i] = 0;
}
for (int i = 0; i < n; ++i)
{
buc[x[i] = init[i]]++;
}
for (int i = 1; i < m; ++i)
{
buc[i] += buc[i - 1];
}
for (int i = n - 1; i >= 0; --i)
{
sa[--buc[x[i]]] = i;
}
for (l = 1, p = 1; l <= n; m = p, l *= 2)
{
p = 0;
for (int i = n - l; i < n; ++i)
{
y[p++] = i;
}
for (int i = 0; i < n; ++i)
{
if (sa[i] >= l)
{
y[p++] = sa[i] - l;
}
}
for (int i = 0; i < m; ++i)
{
buc[i] = 0;
}
for (int i = 0; i < n; ++i)
{
++buc[x[y[i]]];
}
for (int i = 1; i < m; ++i)
{
buc[i] += buc[i - 1];
}
for (int i = n - 1; i >= 0; --i)
{
sa[--buc[x[y[i]]]] = y[i];
}
int i;
for (swap(x, y), x[sa[0]] = 0, p = 1, i = 1; i < n; ++i)
{
x[sa[i]] = cmp(y, sa[i - 1], sa[i], l) ? p - 1 : p++;
}
if (p >= n)
{
break;
}
}
} void getheight()
{
int h = 0;
for (int i = 0; i <= size; ++i)
{
Rank[sa[i]] = i;
}
height[0] = 0;
for (int i = 0; i < size; ++i)
{
if (h > 0)
{
--h;
}
int j = sa[Rank[i] - 1];
for (; i + h < size && j + h < size && init[i + h] == init[j + h]; ++h);
height[Rank[i] - 1] = h;
}
}
bool judge(int k)
{
int maxs = sa[1], mins = sa[1];
for (int i = 1; i < size; ++i)
{
if (height[i] < k)
{
maxs = mins = sa[i + 1];
}
else
{
maxs = max(maxs, sa[i + 1]);
mins = min(mins, sa[i + 1]);
if (maxs - mins > k)
{
return 1;
}
}
}
return 0;
} void solve()
{
int l = 1, r = size;
int mid;
int ans = 0;
while (l <= r)
{
int mid = (l + r) >> 1;
if (judge(mid))
{
l = mid + 1;
ans = mid;
}
else
{
r = mid - 1;
}
}
++ans;
printf("%d\n", ans >= 5 ? ans : 0);
}
}SA; int val[20010]; int main()
{
int n;
while (~scanf("%d", &n), n)
{
SA.clear();
for (int i = 1; i <= n; ++i)
{
scanf("%d", &val[i]);
}
for (int i = n; i >= 2; --i)
{
val[i] = val[i] - val[i - 1] + 90;
}
for (int i = 2; i <= n; ++i)
{
SA.insert(val[i]);
}
SA.getsa();
SA.getheight();
SA.solve();
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

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

  1. poj 1743 Musical Theme (后缀数组+二分法)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16162   Accepted: 5577 De ...

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

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

  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: 27539   Accepted: 9290 De ...

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

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

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

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

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

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

  8. [Poj1743] [后缀数组论文例题] Musical Theme [后缀数组不可重叠最长重复子串]

    利用后缀数组,先对读入整数处理str[i]=str[i+1]-str[i]+90这样可以避免负数,计算Height数组,二分答案,如果某处H<lim则将H数组分开,最终分成若干块,判断每块中是否 ...

  9. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

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

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

随机推荐

  1. iis虚拟目录引发的路径问题

    在iis上把web程序配置成站点是ok的,但配置成虚拟目录,就会发现 图片路径不能,样式不能加载,链接出错. 解决方案: 1,上传图片  ~/upload 2,cs程序,链接跳转,请用~/index. ...

  2. lua-TestMore(转)

    http://fperrad.github.io/lua-TestMore/ http://www.softpedia.com/get/Programming/Debuggers-Decompiler ...

  3. POJ 1384 Piggy-Bank 背包DP

    所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...

  4. LINK : fatal error LNK1181: 无法打开输入文件“..\..\lib\Release\opencv_ocl249.lib”

    最近想要编译什么OpenCV资源.查看源代码调试执行. 按照网上的文章<Win7x64+VS2012+OpenCV2.4.3+CMake2.8.10+TBB41重编译OpenCV> 进行配 ...

  5. 【C语言探索之旅】 第二部分第九课: 实战"悬挂小人"游戏 答案

    内容简介 1.课程大纲 2.第二部分第九课: 实战"悬挂小人"游戏 答案 3.第二部分第十课预告: 安全的文本输入 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题, ...

  6. 用正交多项式作最小二乘拟合的java实现(转)

    import java.util.Scanner; public class Least_square_fit { public static double Least_square_method(i ...

  7. PowerShell 批量导入/导出Active Directory

    PowerShell 批量导入/导出Active Directory         近期由于公司要求,须要导入20个供应商.20个客户到AD域中,刚開始手动添�了2个供应商,2个客户.可是感觉费时费 ...

  8. 64位操作系统下用Microsoft.Jet.OLEDB.4.0出现未注册错误

    在WIN7 64位下用Microsoft.Jet.OLEDB.4.0方法访问数据库Access,出现未注册错误 如果是要建立64位的应用程序 1.Microsoft Access Database E ...

  9. 系列四TortoiseSvn客户端软件

    原文:系列四TortoiseSvn客户端软件 TortoiseSvn介绍 TortoiseSvn 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录.文件保存在中 ...

  10. MariaDb数据库管理系统的学习(一)安装示意图

    MariaDB数据库管理系统是MySQL的一个分支.主要由开源社区在维护,採用GPL授权许可.开发这个分支的原因之中的一个是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区採用分 ...