题目冲鸭:http://poj.org/problem?id=1743

Musical Theme

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 36590   Accepted: 12087

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 的序列,要求找长度不少于 5 的两个不重叠“相似”子串。

(本弱鸡一开始直接以为是找不重叠相同子串,样例都没过)。

相似的定义是长度相等且每一位的数字差都相等。

解题思路:

当然是传统经典口味:后缀数组啦(好吧,就是板子题)

首先处理出 sa 和 height(废话)(怎么处理?@模板)

当然主串就不是输入那个了, 而是相邻两个的值两两作差,得到一个新的主串,

在这个主串里找到两个不重叠相同子串,那么原序列里就对应两个相似主串了(为什么?因为题目要求的相似就是数字差相等嘛)

(不过要注意一点就是在新主串找的两个子串不能紧接在一起,因为这个串是数字差的结果,在原串中就会变成首尾相接了。)

二分可满足的长度 len, 判断是否有满足条件的两个不重叠子串。

判断过程: 先按 height 分组,然后比较组内的 最大的sa 和最小的sa 差值是否满足 len。

AC code:

 //#include<bits/stdc++.h>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define mem(i, j) memset(i, j, sizeof(i))
#define inc(i, j, k) for(int i = j; i <= k; i++)
#define rep(i, j, k) for(int i = j; i < k; i++)
#define gcd(i, j) __gcd(i, j)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e5+;
int r[MAXN];
int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN];
int sa[MAXN]; int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *ws = tmp;
for (i = ; i < m; i++) ws[i] = ;
for (i = ; i < n; i++) ws[x[i] = r[i]]++;
for (i = ; i < m; i++) ws[i] += ws[i - ];
for (i = n - ; i >= ; i--) sa[--ws[x[i]]] = i;
for (j = , p = ; p < n; j *= , m = p)
{
for (p = , i = n - j; i < n; i++) y[p++] = i;
for (i = ; i < n; i++)
if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = ; i < n; i++) wv[i] = x[y[i]];
for (i = ; i < m; i++) ws[i] = ;
for (i = ; i < n; i++) ws[wv[i]]++;
for (i = ; i < m; i++) ws[i] += ws[i - ];
for (i = n - ; i >= ; i--) sa[--ws[wv[i]]] = y[i];
for (swap(x, y), p = , x[sa[]] = , i = ; i < n; i++)
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
}
int Rank[MAXN]; //index range 0~n-1 value range 1~n
int height[MAXN]; //index from 1 (height[1] = 0)
void calheight(int *r, int *sa, int n)
{
int i, j, k = ;
for (i = ; i <= n; ++i) Rank[sa[i]] = i;
for (i = ; i < n; height[Rank[i++]] = k)
for (k ? k-- : , j = sa[Rank[i] - ]; r[i + k] == r[j + k]; ++k);
return;
}
int N, num[MAXN];
bool check(int len, int n)
{
int flag = false;
int mnn = n, mxx = -;
for(int i = ; i <= N; i++){ if((i == N && flag) || (height[i] < len && flag)){
flag = false;
mnn = min(mnn, sa[i-]);
mxx = max(mxx, sa[i-]);
if(mxx-mnn >= len){
return true;
}
mnn = n;
mxx = -;
}
else if(height[i] >= len){
flag = true;
mnn = min(mnn, sa[i-]);
mxx = max(mxx, sa[i-]);
}
}
return false;
} int main()
{
while(~scanf("%d", &N) && N != ){
inc(i, , (N-)) scanf("%d", &num[i]);
rep(i, , (N-)) r[i] = num[i+]-num[i]+;
r[N-] = ;
da(r, sa, N, );
calheight(r, sa, (N-));
// puts("zjj");
int ans = ;
int L = , R = N/, mid;
while(L <= R)
{
mid = (L+R)>>;
if(check(mid, N)){
L = mid+;
ans = max(ans, mid);
}
else R = mid-;
}
if(ans < ) puts("");
else printf("%d\n", ans+);
}
return ;
}

POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】的更多相关文章

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

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

  2. POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )

    题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 :  根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...

  3. Poj 1743 Musical Theme (后缀数组+二分)

    题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...

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

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

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

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

  6. [poj 1743] Musical Theme 后缀数组 or hash

    Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...

  7. POJ 1743 Musical Theme ——后缀数组

    [题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...

  8. POJ 1743 Musical Theme 后缀数组 不可重叠最长反复子串

    二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include & ...

  9. POJ.1743.Musical Theme(后缀数组 倍增 二分 / 后缀自动机)

    题目链接 \(Description\) 给定一段数字序列(Ai∈[1,88]),求最长的两个子序列满足: 1.长度至少为5 2.一个子序列可以通过全部加或减同一个数来变成另一个子序列 3.两个子序列 ...

随机推荐

  1. MINI3内存分配算法

    最差适应算法 #ifdef USING_WORST_FIT { //先找到第一个满足要求的空洞, //再以第一个为标准寻找最适合的空洞. //当最适合的空洞完全吻合 //就直接划给它,当空洞较大时就切 ...

  2. uestc 1709 Binary Operations 位运算的灵活运用

    Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674   Description   B ...

  3. zoj 1048

    这道题提醒了我一个问题,怎么在while(cin>>a[i])下中断 #include<iostream> #include<cmath> #include< ...

  4. 在Pandas中直接加载MongoDB的数据

    在使用Pandas进行数据处理的时候,我们通常从CSV或EXCEL中导入数据,但有的时候数据都存在数据库内,我们并没有现成的数据文件,这时候可以通过Pymongo这个库,从mongoDB中读取数据,然 ...

  5. Java集合 之Map(HashMap、Hashtable 、TreeMap、WeakHashMap )理解(new)

    HashMap 说明: 在详细介绍HashMap的代码之前,我们需要了解:HashMap就是一个散列表,它是通过“拉链法”解决哈希冲突的.还需要再补充说明的一点是影响HashMap性能的有两个参数:初 ...

  6. myeclipse 10安装之后该做些什么?

    @破解  http://files.cnblogs.com/files/zyuqiang/MyEclipse%E7%A0%B4%E8%A7%A3%E6%96%87%E4%BB%B6.rar @修改字体 ...

  7. SqlServer代理(已禁用代理xp)

    SqlServer 本地库作业管理的时候已禁用,将其修改为可使用,master数据库下执行以下语句: sp_configure 'show advanced options', 1;  GO  REC ...

  8. 理解webpack4.splitChunks之chunks

    上回说到按照默认的splitChunks配置,入口里面的第三方依赖没有打包出来,这个是因为chunks属性的原因,下面我们就介绍chunks属性的意义和用法. chunks的含义是拆分模块的范围,它有 ...

  9. css专业术语笔记

    1. 属性 如height.color等,称作css的属性. 2. 值 在css中,如:10px, 50%, #ccc等这些都称作css的值.比较常见的类型值有:整数值,数值,百分比值,长度值,颜色值 ...

  10. webform 使用log4net配置

    效果: web.config配置 <configuration> <configSections> <!--log4net日志记录--> <section n ...