【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 ...
随机推荐
- Prometheus Node_exporter 之 Network Netstat UDP
Network Netstat UDP /proc/net/snmp 1. UDP In / Out type: GraphUnit: shortLabel: Datagrams out (-) / ...
- commons-pool 解析
首先抛出个常见的长连接问题: 1 都知道连接MySQL的应用中大多会使用框架例如 c3p0 ,dbcp proxool 等来管理数据库连接池. 数据库连接池毫无疑问都是采用长连接方式. 那么MySQ ...
- T-SQL查询两个日期之间的休息日(周六周日)天数
SELECT COUNT(*) FROM MASTER..SPT_VALUES WHERE TYPE = 'P' AND DATEADD(DAY,NUMBER,'2014-03-13')<='2 ...
- nmcli 命令使用
nmcli 是 NetworkManager 的控制客户端,通过 man 查看可知: NMCLI(1) General Commands Manual NMCLI(1) NAME nmcli - co ...
- spring mvc 接收 put参数
web.xml中: <!-- 用户put提交参数 --> <filter> <filter-name>HttpMethodFilter</filter-nam ...
- MySQL主从复制异步原理以及搭建
MySQL主从复制的原理: 1.首先,MySQL主库在事务提交时会把数据变更作为时间events记录在二进制日志文件binlog中:MySQL主库上的sync_binlog参数控制Binlog日志以什 ...
- 每年有20万人进军IT行业,为何还会人才短缺?
众所周知,IT行业是个高薪行业,也是很多人的梦想职业,在全球最缺人的十大行业中IT行业居首位. 但是现在很多人都有一个疑问: 几乎每所大学里都有计算机技术相关专业,再加上IT培训机构的输出,每年培养出 ...
- 【转】Python操作MongoDB数据库
前言 MongoDB GUI 工具 PyMongo(同步) Motor(异步) 后记 前言 最近这几天准备介绍一下 Python 与三大数据库的使用,这是第一篇,首先来介绍 MongoDB 吧,,走起 ...
- jenkins发版脚本更新
jenkins 项目名中明确了 是jar tar.gz war包研发需要提供 项目名-地点-环境(研发.测试.生产)-应用项目名称(admin)-包格式(jar\war\gz) deployment ...
- linux问题解答
1.Linux如何查询进程?杀死一个进程? ps命令提供进程的一次性查看(瞬时信息),结果不是动态的:top对命令实时监控 ps只是查看进程,而top还可以监视系统性能,如平均负载,cpu和内存的消耗 ...