题目大意

直接用了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. SummerVocation_Learning--java的线程死锁

    public class Test_DeadLock implements Runnable { ; static Object o1 = new Object(),o2 = new Object() ...

  2. BootStrap下拉框搜索功能

    <!DOCTYPE html> <html> <head> <title>jQuery bootstrap-select可搜索多选下拉列表插件-www. ...

  3. jQuery的三种写法

    jQuery的三种写法 jQuery一共有三种写法,写法如下: <script type="text/javascript" src="js/jquery-1.9. ...

  4. 15.VUE学习之-表单中使用key唯一令牌解决表单值混乱问题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. String java问题随笔

    1.字符串加密 设计思想: 每个字符都能够转化为整数型,也能将整数型转化为字符类型,这样我们在加密时候由于向后推3个,所以可以将字符转换为整形,然后加3,之后在将运算完的变量转化为字符后输出,就可以实 ...

  6. (新手)使用pandas操作EXCEL

    import pandas as pdimport numpy as npfrom pandas import DataFrame,Series#path = r'C:\Users\tsl\Deskt ...

  7. I Like for You to Be Still【我会一直喜欢你】

    I Like for You to Be Still I like for you to be still 我会一直喜欢这你 It is as though you are absent 就算你并不在 ...

  8. Linux系统软件包之---Apache

    当前互联网主流web服务说明 静态服务: apache 中小型静态web服务的主流,web服务器中的老大哥 nginx 大型新兴网站静态web服务主流,web服务器中的初生牛犊 lighttpd 静态 ...

  9. 2 Model层 -定义模型

    1  ORM简介 MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库 ORM是“对象-关系-映射” ...

  10. Falsk

    flask: 1.配置文件的几种方式: 1.app.config['DEBUG'] =True 2.app.config.from_pyfile("setting.py") 3.a ...