题意

给出一个n个数字的序列,找出相同变化趋势且不重叠的两个最长子串。

分析

这个题以前应该用后缀数组+二分做过。学了后缀自动机后可以用后缀自动机搞一下。

先差分,然后把查分后的数组建SAM。然后对于每个状态记录一个l[u],和r[u],分别代表right集合中,最大的v和最小的v。(这里如果不明白可以去看clj的课件)。

然后对于每个状态,当这个状态cnt[u]>=2的时候,说明有两个以上的子串,然后min(st[u].len,r[u]-l[u])就是这个状态最长不重叠相同子串的长度。

 #include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
const int maxn=+;
const int INF=;
int s[maxn],a[maxn],n;
struct state{
int len,link;
int next[];
}st[*maxn];
int last,cur,sz;
int cnt[*maxn],l[*maxn],r[*maxn],c[*maxn];
void init(){
sz=;
last=cur=;
st[].len=;
st[].link=-;
memset(st[].next,,sizeof(st[].next));
} void build_sam(int c,int pos){
cur=sz++;
st[cur].len=st[last].len+;
cnt[cur]=;
l[cur]=r[cur]=pos;
memset(st[cur].next,,sizeof(st[cur].next));
int p;
for(p=last;p!=-&&st[p].next[c]==;p=st[p].link)
st[p].next[c]=cur;
if(p==-)
st[cur].link=;
else{
int q=st[p].next[c];
if(st[q].len==st[p].len+)
st[cur].link=q;
else{
int clone=sz++;
cnt[clone]=r[clone]=;
l[clone]=;
st[clone].len=st[p].len+;
//printf("%d ",st[clone].len);
st[clone].link=st[q].link;
memcpy(st[clone].next,st[q].next,sizeof(st[clone].next));
for(;p!=-&&st[p].next[c]==q;p=st[p].link){
st[p].next[c]=clone;
}
st[cur].link=st[q].link=clone;
}
}
last=cur;
}
int cmp(int a,int b){
return st[a].len>st[b].len;
} int ans=;
int main(){
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<=n;i++){
scanf("%d",&s[i]);
a[i]=s[i]-s[i-];
}
init();
for(int i=;i<=n;i++){
build_sam(a[i]+,i);
//printf("%d ",a[i]);
}
// for(int i=0;i<sz;i++)
// printf("%d ",st[i].len);
// printf("\n");
for(int i=;i<sz;i++)
c[i]=i;
sort(c,c+sz,cmp);
ans=;
for(int i=;i<sz;i++){
int o=c[i];
if(st[o].link!=-){
cnt[st[o].link]+=cnt[o];
l[st[o].link]=min(l[st[o].link],l[o]);
r[st[o].link]=max(r[st[o].link],r[o]);
}
// printf("%d %d %d %d\n",cnt[o],st[o].len,l[o],r[o]);
if(cnt[o]>=&&min(st[o].len,r[o]-l[o])>=){
//printf("%d %d\n",l[o],r[o]);
ans=max(ans,min(st[o].len,r[o]-l[o]));
}
}
if(ans<)
printf("0\n");
else
printf("%d\n",ans+);
}
return ;
}

【poj1743】Musical Theme 【后缀自动机】的更多相关文章

  1. POJ1743 Musical Theme [后缀自动机]

    题意:不重叠最长重复子串 后缀数组做法:http://www.cnblogs.com/candy99/p/6227659.html 后缀自动机的话,首先|Right|>=2 然后min(t[u] ...

  2. POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Tot ...

  3. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  4. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  5. poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树

    题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...

  6. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

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

    题目大意:给一个整数序列,找出最长的连续变化相同的.至少出现两次并且不相重叠一个子序列. 题目分析:二分枚举长度进行判定. 代码如下: # include<iostream> # incl ...

  8. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

  9. POJ1743 Musical Theme (后缀数组 & 后缀自动机)最大不重叠相似子串

    A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...

  10. poj1743 Musical Theme【后缀数组】【二分】

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 35044   Accepted: 11628 D ...

随机推荐

  1. nginx 获取请求头,URL参数

    获取url参数 在 ngx_lua 中访问 Nginx 内置变量 ngx.var.arg_PARAMETER 即可获得GET参数PARAMETER的内容. 在 nginx配置中,通过$arg_PARA ...

  2. 微信开发 api 需要 https 服务器

    微信开发 api 需要 https 服务器 先建一个环境,本地的 https 服务器. 以下这篇不错,很完整. https://zhuanlan.zhihu.com/p/23640321

  3. JavaSE 手写 Web 服务器(一)

    原文地址:JavaSE 手写 Web 服务器(一) 博客地址:http://www.extlight.com 一.背景 某日,在 Java 技术群中看到网友讨论 tomcat 容器相关内容,然后想到自 ...

  4. 简单的使用POI导出excel

    package org.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java ...

  5. Hibernate学习7—Hibernate 映射继承

    需求:学生有很多照片,分为生活照和工作照: 第一节:每个具体类对应一个表 Student.java: package com.cy.model; import java.util.Set; publi ...

  6. python--logging库学习_第三波

    本文介绍如何写一个Python日志类,用来输出不同级别的日志信息到本地文件夹下的日志文件里.为什么需要日志输出呢,我们需要记录我们测试脚本到底做了什么事情,最好的办法是写事件监听.这个事件监听,对我们 ...

  7. 怎样用java生成GUID与UUID

    GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随机数来生成GUID.从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义 ...

  8. python学习 (三十四) Python文件操作

    1 写文件 my_list = ["] my_file = open("myfile.txt", "w") for item in my_list: ...

  9. Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入(1)

    一.本系列分为6部分 1.Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入 2.Centos 6.5 下安装 Zabbix server 3.0服务器的安 ...

  10. 开启 3389 的 cmd 命令

    方法一: 测试环境 Windows 2003 server 查看开启的端口 没有开启 3389 端口 执行语句 wmic RDTOGGLE WHERE ServerName='%COMPUTERNAM ...