题目冲鸭: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. JD上市前内情:李彦宏雷军柳传志拷问刘强东

    这篇文章是京东上市前夕,在某个会议上刘强东与柳传志.李彦宏.雷军等大佬们的闭门交流实录,由于当时京东正值上市敏感期,文章没有被发出来,现在京东上市了,我想,大家可以看看几位商界大佬对刘强东的“犀利拷问 ...

  2. linux下elasticsearch集成mongodb详细教程

    由于公司业务需要,要用elasticsearch做索引库实现搜索功能,历尽千辛万苦,最后总算把mongodb和elasticsearch集成成功 1.搭建mongodb集群 参考https://www ...

  3. 5 springboot 集成dubbo

    Apache Dubbo 是一款高性能Java RPC框架 由阿里巴巴开源并进入Apache孵化器,官网 http://dubbo.apache.org 提供服务化基础功能: 接口远程调用,智能负载均 ...

  4. 网络安全之——DNS欺骗实验

        ---------------发个帖证明一下存在感,希望各位大牛们,别喷我!!谢谢--------------         DNS(域名系统)的作用是把网络地址(域名,以一个字符串的形式) ...

  5. Hunger Snake

    除了驱动的效果.

  6. linux-ubuntu安装配置uwsgi

    参考原文 对于 Python2.x 版本:(测试通过) 第一步:sudo apt-get install python-dev 第二步:sudo apt-get install python-pip  ...

  7. CNN中卷积过程中padding的使用

    1.podding='SAME'时,全0填充.     2.padding=“VALID”,不使用全0填充      

  8. 【PyQt5 学习记录】002:添加部件及网格布局

    #!/usr/bin/python3 # -*- coding:utf-8 -*- import sys from PySide2.QtWidgets import (QApplication, QW ...

  9. 转:问题解决:The project cannot be built until build path errors are resolved

    转自:http://blog.csdn.net/marty_zhu/article/details/2566299 今天在eclipse里遇到这个问题,之前也遇到过,不过,通过clean一下项目,或者 ...

  10. 封装网络请求并在wxml调用

    https://blog.csdn.net/qq_35713752/article/details/78109084 // url:网络请求的url method:网络请求方式 data:请求参数 m ...