【SP1812】LCS2 - Longest Common Substring II
【SP1812】LCS2 - Longest Common Substring II
题面
题解
你首先得会做这题。
然后就其实就很简单了,
你在每一个状态\(i\)打一个标记\(f[i]\)表示状态\(i\)能匹配到最长的子串长度,
显然\(f[i]\)可以上传给\(f[i.fa]\)。
然后去每个串和第\(1\)个串\(f\)的最小值的最大值即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX_N = 1e5 + 5;
struct Node { int ch[26], fa, len; } t[MAX_N << 1];
int tot = 1, lst = 1;
void extend(int c) {
++tot, t[lst].ch[c] = tot;
t[tot].len = t[lst].len + 1;
int p = t[lst].fa; lst = tot;
while (p && !t[p].ch[c]) t[p].ch[c] = tot, p = t[p].fa;
if (!p) return (void)(t[tot].fa = 1);
int q = t[p].ch[c];
if (t[q].len == t[p].len + 1) return (void)(t[tot].fa = q);
int _q = ++tot; t[_q] = t[q];
t[_q].len = t[p].len + 1, t[q].fa = t[tot - 1].fa = _q;
while (p && t[p].ch[c] == q) t[p].ch[c] = _q, p = t[p].fa;
}
char a[MAX_N];
int N, A[MAX_N << 1], f[MAX_N << 1], g[MAX_N << 1], bln[MAX_N << 1];
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
scanf("%s", a + 1);
N = strlen(a + 1);
for (int i = 1; i <= N; i++) extend(a[i] - 'a');
for (int i = 1; i <= tot; i++) ++bln[t[i].len];
for (int i = 1; i <= tot; i++) bln[i] += bln[i - 1];
for (int i = 1; i <= tot; i++) A[bln[t[i].len]--] = i;
for (int i = 1; i <= tot; i++) g[i] = t[i].len;
while (scanf("%s", a + 1) != EOF) {
N = strlen(a + 1);
for (int i = 1; i <= tot; i++) f[i] = 0;
for (int v = 1, l = 0, i = 1; i <= N; i++) {
while (v && !t[v].ch[a[i] - 'a']) v = t[v].fa, l = t[v].len;
if (!v) v = 1, l = 0;
if (t[v].ch[a[i] - 'a']) ++l, v = t[v].ch[a[i] - 'a'];
f[v] = max(f[v], l);
}
for (int i = tot; i; i--) f[t[i].fa] = max(f[t[i].fa], f[i]);
for (int i = 1; i <= tot; i++) g[i] = min(g[i], f[i]);
}
printf("%d\n", *max_element(&g[1], &g[tot + 1]));
return 0;
}
【SP1812】LCS2 - Longest Common Substring II的更多相关文章
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
- SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 题解 SP1812 【LCS2 - Longest Common Substring II 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
- 【刷题】SPOJ 1812 LCS2 - Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
随机推荐
- 使用thumbnailator不按照比例,改变图片的大小
我们在平时的开发中,偶尔也会遇到图片处理的问题,比如图片的压缩,按比例改变图片的大小,不按比例改变图片的大小等等. 如果要自己去开发这样一套工具,我觉得大多数人都是做不到的,所以还是学会站在巨人的肩膀 ...
- 从零自学Java-5.使用条件测试进行判断
1.使用if语句进行最基本的条件测试:2.测试一个值大于还是小于另一个值:3.测试两个值是否相等:4.使用与if语句对应的else语句:5.组合多个条件测试:6.使用switch语句进行复杂的条件测试 ...
- Ionic命令大全
start [options] <PATH> [template] ............. Starts a new Ionic project in the specified P ...
- 适用于 Windows VM 的 Azure 示例基础结构演练
本文是更广泛系列的一部分,当你在 Azure 中构建应用程序基础结构时,这些系列向你提供设计注意事项和准则. 用户可以查看系列中的其他主题. 尽管用户可以在 Azure 中快速地构建出开发/测试环境, ...
- jbosscache
JBossCache 讲解说明 是什么? 一个树形结构.支持集群.支持事务的缓存技术. 有什么作用? JBoss Cache是针对Java应用的企业级集群解决方案,其目的是通过缓存需要频繁访问的Jav ...
- Sql Server与.Net(C#)中星期值对比
最近发现Sql Server与.Net(C#)中星期值居然不匹配,倒不知道依哪一个了. 1.Sql Server declare @date datetime; set @date = '2017-0 ...
- Automation Script For Percona Xtrabackup FULL/Incremental
This is my first post in 2019, and Im starting with a MySQL solution. In MySQL world, implementing a ...
- MySQL MTS复制: hitting slave_pending_jobs_size_max
测试步骤: 从库停止复制:stop slave; 主库创建大表400万条记录. 开启从库复制:start slave; 监测从库error log持续输出: 2018-12-06T10:40:52.6 ...
- mySQL 约束 (Constraints):一、非空约束 NOT NULL 约束
非空约束 NOT NULL 约束: 强制列不能为 NULL 值,约束强制字段始终包含值.这意味着,如果不向字段添加值,就无法插入新记录或者更新记录. 1.在 "Persons" 表 ...
- div中嵌套div水平居中,垂直居中
方法一: div(父):display:table; div(子):display:table_cell;margin:0 auto;vertical-align:middle; 方法二: div(父 ...