POJ - 1743 后缀自动机
顺着原字符串找到所有叶子节点,然后自下而上更新,每个节点right的最左和最右,然后求出答案。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define PDD pair<double,double>
#define ull unsigned long long
using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const int base = ; int n;
int s[N], a[N]; struct SuffixAutomaton {
int last, cur, cnt, ch[N<<][], id[N<<], fa[N<<], dis[N<<], sz[N<<], c[N];
int mx[N<<], mn[N<<];
SuffixAutomaton() {cur = cnt = ;}
void init() {
for(int i = ; i <= cnt; i++) {
memset(ch[i], , sizeof(ch[i]));
sz[i] = c[i] = dis[i] = fa[i] = ;
}
cur = cnt = ;
}
void extend(int c, int id) {
last = cur; cur = ++cnt;
int p = last; dis[cur] = id;
for(; p && !ch[p][c]; p = fa[p]) ch[p][c] = cur;
if(!p) fa[cur] = ;
else {
int q = ch[p][c];
if(dis[q] == dis[p]+) fa[cur] = q;
else {
int nt = ++cnt; dis[nt] = dis[p]+;
memcpy(ch[nt], ch[q], sizeof(ch[q]));
fa[nt] = fa[q]; fa[q] = fa[cur] = nt;
for(; ch[p][c]==q; p=fa[p]) ch[p][c] = nt;
}
}
sz[cur] = ;
}
void getSize(int n) {
for(int i = ; i <= cnt; i++) c[dis[i]]++;
for(int i = ; i <= n; i++) c[i] += c[i-];
for(int i = cnt; i >= ; i--) id[c[dis[i]]--] = i;
for(int i = cnt; i >= ; i--) {
int p = id[i];
sz[fa[p]] += sz[p];
}
}
void solve(int *s, int n) {
memset(mx, , sizeof(int)*(cnt+));
memset(mn, inf, sizeof(int)*(cnt+));
for(int i = , p = ; i <= n; i++) {
p = ch[p][s[i]];
mx[p] = mn[p] = i;
}
for(int i = cnt; i >= ; i--) {
int p = id[i];
mx[fa[p]] = max(mx[fa[p]], mx[p]);
mn[fa[p]] = min(mn[fa[p]], mn[p]);
}
int ans = ;
for(int i = ; i <= cnt; i++) {
ans = max(ans, min(mx[i]-mn[i], dis[i]));
}
if(ans < ) puts("");
else printf("%d\n", ans+);
}
} sam; int main() {
while(scanf("%d", &n) != EOF && n) {
sam.init();
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i < n; i++) s[i] = a[i+]-a[i]+base;
for(int i = ; i < n; i++) sam.extend(s[i], i);
sam.getSize(n-);
sam.solve(s, n-);
}
return ;
} /*
*/
POJ - 1743 后缀自动机的更多相关文章
- POJ 3518 (后缀自动机)
POJ 3518 Boring Problem : 给一个串S,询问串S有多个子串出现至少两次且位置不重叠. Solution : 对S串建立后缀自动机,再建立后缀树,dfs一遍统计处每个结点的子树中 ...
- POJ 3415 (后缀自动机)
POJ 3415 Common Substrings Problem : 给两个串S.T (len <= 10^5), 询问两个串有多少个长度大于等于k的子串(位置不同也算). Solution ...
- POJ 1743 后缀数组
题目链接:http://poj.org/problem?id=1743 题意:给定一个钢琴的音普序列[值的范围是(1~88)],现在要求找到一个子序列满足 1,长度至少为5 2,序列可以转调,即存在两 ...
- POJ 1743 (后缀数组+不重叠最长重复子串)
题目链接: http://poj.org/problem?id=1743 题目大意:楼教主の男人八题orz.一篇钢琴谱,每个旋律的值都在1~88以内.琴谱的某段会变调,也就是说某段的数可以加减一个旋律 ...
- poj 1743 后缀数组 最长不重叠子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30941 Accepted: 10336 D ...
- poj 1743 后缀数组 求最长不重叠重复子串
题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...
- [八分之一的男人]POJ - 1743 后缀数组 height分组 带详解
题意:求最长不可重叠的相同差值子串的长度 这道题算是拖了好几个月,现在花了点时间应该搞懂了不少,尝试分析一下 我们首先来解决一个退化的版本,求最长不可重叠的相同子串(差值为0) 比如\(aabaaba ...
- Musical Theme POJ - 1743 后缀数组
A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...
- POJ 1743 后缀数组不重叠最长重复子串
#include<stdio.h> #include<string.h> #include<algorithm> #define maxn 30000 using ...
随机推荐
- Linux安装python模块
# 依赖解决 # yum -y install epel-release python-devel gcc # 安装模块 # pip install psutil (若没有pip命令,可通过 yum ...
- MYSQL 在当前时间加上或减去一个时间段
update user set time1=now(),time2=date_add(NOW(), interval 1 MONTH) where id=1; date_add() 增加date_su ...
- 11 Facts about Data Science that you must know
11 Facts about Data Science that you must know Statistics, Machine Learning, Data Science, or Analyt ...
- JavaScript 数组元素排序
var sortArray = new Array(3,6,8888,66); // 元素必须是数字 sortArray.sort(function(a,b){return a-b}); // a-b ...
- Spring Cloud(十一)声名式服务调用:Feign的使用 (上)
一.写在前边 最近开发任务比较忙,下班也开始锻炼了,这个系列的文章就放了很久,看github我提交的Feign的入门程序已经好久了,今天正好得空,这就更上一贴,准备分几部分写 注意:之前几个项目中,笔 ...
- IE6 下 DD_belatedPNG 引发的血案
群里一朋友Q我,说遇到兼容性问题了,我说为何不用jQuery呢(因为他们公司要求尽量js写).他说用了,还是有问题,IE6下不行,其他都行.然后他发我代码,我一开始真以为是兼容性问题,比如数组对象最后 ...
- 转载http中302与301的区别
http://blog.csdn.net/qmhball/article/details/7838989 一.官方说法301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于 ...
- HDU 2058 The sum problem 数学题
解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...
- HDU 1717 小数化分数2 数学题
解题报告:输入一个小于1的小数,让你把这个数转化成分数,但注意,输入的数据还有无限循环的小数,循环节用一对括号包含起来. 之前还没有写过小数转分数的题,当然如果没有循环小数的话,应该比较简单,但是这题 ...
- 【leetcode 简单】 第六十六题 用栈实现队列
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...