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下vim和bash配置文件
1.注释版 ~/.vimrc "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限 set nocompatible set autoread " 文件修改之后自动载入 ...
- POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图
题目:http://poj.org/problem?id=3259 题意:主要就是构造图, 然后判断,是否存在负图,可以回到原点 /* 2 3 3 1 //N, M, W 1 2 2 1 3 4 2 ...
- Linux启动的流程
2017年1月10日, 星期二 Linux启动的流程 null
- JavaScript 数组元素排序
var sortArray = new Array(3,6,8888,66); // 元素必须是数字 sortArray.sort(function(a,b){return a-b}); // a-b ...
- Minicap使用分析
想起前段时间研究过的minicap,抱着无果的心情再次看了源码,这次竟然比上次清晰了一点点,难道是因为这两天被android源码折磨得身心疲惫然而却在不知不觉中增长了?不懂怎么样,看懂了大概. Min ...
- 【COGS】2287:[HZOI 2015]疯狂的机器人 FFT+卡特兰数+排列组合
[题意][COGS 2287][HZOI 2015]疯狂的机器人 [算法]FFT+卡特兰数+排列组合 [题解]先考虑一维的情况,支持+1和-1,前缀和不能为负数,就是卡特兰数的形式. 设C(n)表示第 ...
- Collections -- 集合的工具类
Collections是JDK针对集合提供的一个工具类,他提供一系列静态方法实现对各种集合的搜索.排序.线程安全化等操作. 1.搜索 如可以使用Collections提供的二分查找方法binarySe ...
- device tree --- #address-cells and #size-cells property【转】
转自:http://www.cnblogs.com/youchihwang/p/7050846.html device tree source Example1 / { #address-cells ...
- gpio口、内核定时器使用
/*申请gpio*/ int gpio_request(unsigned gpio, const char *label); /*设置gpio为输入状态,即设置如(GPH0CON)*/ int gpi ...
- @RequestParam,@PathParam,@PathVariable,@QueryParam注解的使用区别
获取url模板上数据的(/{id})@DefaultValue 获取请求参数的(包括post表单提交)键值对(?param1=10¶m2=20).可以设置defaultValue JA ...