poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树
题目大意
直接用了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 后缀自动机/后缀数组/后缀树的更多相关文章
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)
永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...
- poj 1743 Musical Theme(最长重复子串 后缀数组)
poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
- POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- poj 1743 Musical Theme (后缀数组+二分法)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16162 Accepted: 5577 De ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- [poj 1743] Musical Theme 后缀数组 or hash
Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...
- POJ - 1743 Musical Theme (后缀数组)
题目链接:POJ - 1743 (不可重叠最长子串) 题意:有N(1<=N<=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的子串,它需要 ...
随机推荐
- iOS多播Delegate类——GCDMulticastDelegate用法小结
iOS中通常的delegate模式只能有一个被委托的对象,这样当需要有多个被委托的对象时,实现起来就略为麻烦,在开源库XMPPFramework中提供了一个GCDMulticastDelegate类, ...
- phpstudy iis版本 php4.4.5 和 php5.6.7目录权限问题
开始用的php4.4.5 +iis 权限设置好了,切换成php5.6.7后目录没有了写入权限,各种百度后未能解决 php4.4.5 +iis 时 iis 匿名身份验证 用户是 IUSR 目 ...
- JZOJ 1264. 乱头发节
1264. 乱头发节(badhair.pas/c/cpp) (File IO): input:badhair.in output:badhair.out Time Limits: 1000 ms M ...
- 21.VUE学习之-操作data里的数组变异方法push&unshit&pop&shift的实例应用讲解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ecshop里添加多个h1标题
目录 功能: 思路: 效果: pageheader_list.htm里 product_sn_list.htm模板里 控制器里 功能: 点击页面右边的两个按钮,切换下面的<div class=& ...
- Pychram基本操作
1. 更改pychram页面为黑色背景主题.更改主题: File ->Settings -> Editor -> Color Scheme -> Scheme -> Mo ...
- day 52 Django基础一之web框架的本质
Django基础一之web框架的本质 django第一天 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于D ...
- 洛谷P2389 电脑班的裁员(区间DP)
题目背景 隔壁的新初一电脑班刚考过一场试,又到了BlingBling的裁员时间,老师把这项工作交给了ZZY来进行.而ZZY最近忙着刷题,就把这重要的任务交(tui)给了你. 题目描述 ZZY有独特的裁 ...
- open()函数之文件操作
#open() 文件操作 #打开文件的模式有: r,只读模式[默认] w,只写模式[不可读:不存在则创建:存在则清空内容:] x,只写模式[不可读:不存在则创建,存在则报错] a,追加模式[可读:不存 ...
- Android stadio 调试太掉了
1.evalute expresstion 可以看任何变量的任何属性,就算是一个字符串url,你可以url.length(),你不用输入完就有提示.对象的方法有提示! 2.调试技巧 就是当一行里面有很 ...