题目大意

直接用了hzwer的题意

题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:

1.长度至少为5个音符。

2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

3.重复出现的同一主题不能有公共部分。

分析

对于区间加一个数也算相同

转化一下就变成相邻两数差相同

变成问重复两次的不相交的子串最长是多少

做法1

sa,二分+判断,记录组内最大最小值就好

做法2

sam,搞出后缀树

一个节点\(x\)长度可以是\([1..max(x)]\)

用\(max(x)\)和\(x\)的right集中最大减最小比一下取min就好

注意

答案小于5输出0

转化问题后不相交变成了两个子串中间至少一个间隔

姿势

以后后缀自动机/后缀树 用这些变量名

int last,tot;
int ch[M][N];
int stp[M];
int fa[M];
int right[M];
int hg[M],tg;
struct suftree{
int y,nxt;
}go[M];

last和tot在main里初始化

多组数据注意初始化last,tot,ch,right

solution

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cmath>
using namespace std;
const int N=180;
const int M=40007;
const int INF=0x3f3f3f3f; inline int rd(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
} int n;
int a[M];
int ans; int last,tot;
int ch[M][N];
int stp[M];
int fa[M];
int right[M];
int mx[M],mn[M];
int hg[M],tg;
struct suftree{
int y,nxt;
}go[M]; int newnode(int ss){
stp[++tot]=ss;
return tot;
} int ext(int p,int q,int d){
int nq=newnode(stp[p]+1);
fa[nq]=fa[q];
fa[q]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
return nq;
} int sam(int p,int d){
int np=ch[p][d];
if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d); np=newnode(stp[p]+1);
for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
if(!p) fa[np]=1;
else{
int q=ch[p][d];
fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
}
return np;
} void addgo(int x,int y){
go[++tg].y=y; go[tg].nxt=hg[x]; hg[x]=tg;
} void dfs(int x){
int p,y;
if(!right[x]) mx[x]=0, mn[x]=INF;
else mx[x]=mn[x]=right[x];
for(p=hg[x];p;p=go[p].nxt){
y=go[p].y;
dfs(y);
mx[x]=max(mx[x],mx[y]);
mn[x]=min(mn[x],mn[y]);
}
ans=max(ans,min(stp[x],mx[x]-mn[x]-1));
} int main(){ int i; while((n=rd())!=0){
memset(right,0,sizeof(right));
memset(ch,0,sizeof(ch));
last=tot=1;
for(i=1;i<=n;i++) a[i]=rd();
for(i=1;i<n;i++) a[i]=a[i+1]-a[i];
n--;
for(i=1;i<=n;i++){
last=sam(last,a[i]+88);
right[last]=i;
}
memset(hg,0,sizeof(hg)); tg=0;
for(i=2;i<=tot;i++) addgo(fa[i],i);
ans=0;
dfs(1);
ans++;
if(ans<5) puts("0");
else printf("%d\n",ans);
} return 0;
}

poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树的更多相关文章

  1. POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)

    永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...

  2. poj 1743 Musical Theme(最长重复子串 后缀数组)

    poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...

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

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

  4. POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】

    题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Su ...

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

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

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

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

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

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

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

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

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

    题目链接:POJ - 1743   (不可重叠最长子串) 题意:有N(1<=N<=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的子串,它需要 ...

随机推荐

  1. Math类小结

    package com.swift; public class MathDemo { public static void main(String[] args) { // TODO Auto-gen ...

  2. jstl(c)标签

    一.EL表达式: Expression Language提供了在 JSP 脚本编制元素范围外(例如:脚本标签)使用运行时表达式的功能.脚本编制元素是指页面中能够用于在JSP 文件中嵌入 Java 代码 ...

  3. GNU汇编 函数调用的例子

    .text .global  _start _start: mov r1,#2 cmp  r1,#1 bl func1    @bl能保存下一条指令的位置到lr寄存器里面,b不能 mov  r1, # ...

  4. Ansible学习 安装

    对于运维人员来说,自动化工具是日常工作中比不可少的.Ansible是一个很好的自动化工具. Ansible默认使用SSH协议管理机器,在管理主机上安装Ansible,管理主机和被管理主机只要安装了py ...

  5. JZOJ 1266. 玉米田

    1266. 玉米田(cowfood.pas/c/cpp) (File IO): input:cowfood.in output:cowfood.out Time Limits: 1000 ms  Me ...

  6. python爬虫的基本思路

    爬虫:请求网站并提取数据的自动化程序. 流程: 发送请求 -> 获取数据 -> 解析数据 -> 存储数据

  7. 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8

    a,b,c,d,e,f,g=1,2,3,4,5,8,9 m = a > b and c < d or c > e n = b > a or g < f x = m and ...

  8. Python9-MySQL-MySQL-ORM框架-day48

    ORM框架:AQLAlchemy-作用: 1.提供简单的规则 2.自动转换成SQL语句 -DB first: 手动创建数据库以及表 -> ORM框架 -> 自动生成类 code first ...

  9. Air Pollution【空气污染】

    Air Pollution Since the 1940s, southern California has had a reputation for smog. 自20世纪40年代以来,南加利福尼亚 ...

  10. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...