题目冲鸭: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 ;
}

最新文章

  1. react-native 环境搭建以及项目创建打包
  2. 微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}
  3. mysql 正则篇
  4. jdbc执行预处理,批处理,LOB字段处理,调用存储过程
  5. R包之间冲突带来的奇怪错误
  6. How do I set the default schema for a user in MySQL
  7. MySQL模糊查询:LIKE模式和REGEXP模式
  8. c#的序列化与反序列化
  9. &ldquo;耐撕&rdquo;团队记账本 剧透
  10. Axis2的下载和安装
  11. JQuery Basic Features Quick Walkthrough
  12. 一个机器学习博客 ,包括 Standford公开课machine learning
  13. 【CPP】数据和C
  14. hibernate flushMode 错误
  15. 【安卓开发】Facebook工程师是如何改进他们Android客户端的
  16. 我的CSS
  17. Java核心技术及面试指南 流程控制方面的面试题答案
  18. zabbix系列 ~ linux监控相关
  19. Linux(5.5版为主)的基本操作命令
  20. toString 和new String()区别

热门文章

  1. php 函数的嵌套
  2. yum卸载
  3. ROS:消息发布器和订阅器(c++)
  4. jvm工具及命令大全
  5. 以面向对象的思想实现数据表的添加和查询,JDBC代码超详细
  6. 后台数据校验-BeanCheck
  7. springboot开篇 (一)简单邮件发送
  8. 精选10款HTML5手机模板
  9. IOS CALayer的属性和使用
  10. Android获取蓝牙地址