【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 ...
随机推荐
- 使用 Chef 自动执行 Azure 虚拟机部署
Chef 是一个强大的工具,用于提供自动化和所需的状态配置. 使用我们的最新 cloud-api 版本,Chef 提供了与 Azure 的无缝集成,使得你能够通过单个命令设置和部署配置状态. 在本文中 ...
- 利用percona-toolkit定位数据库性能问题
当你的性能瓶颈卡在数据库这块的时候,可以通过percona-toolkit来进行问题定位. 那么,首先,介绍下percona-toolkit.percona-toolkit是一组高级命令行工具的集合, ...
- MySQL查询计划 key_len计算方法
本文首先介绍了MySQL的查询计划中ken_len的含义:然后介绍了key_len的计算方法:最后通过一个伪造的例子,来说明如何通过key_len来查看联合索引有多少列被使用. key_len的含义 ...
- 远程桌面web连接
我们可以利用web浏览器搭配远程桌面技术来连接远程计算机,这个功能被称为远程桌面web连接(Remote desktop web connection),要享有此功能,请先在网络上一台window ...
- Python实例---爬去酷狗音乐
项目一:获取酷狗TOP 100 http://www.kugou.com/yy/rank/home/1-8888.html 排名 文件&&歌手 时长 效果: 附源码: import t ...
- windows服务器安装telnet的方法指引
摘要: 1.telnet是一种网络排查的工具 2.当发现一台服务器异常的时候,通常有两个cmd命名做排查 3.ping 服务器ip,看网络是否联通 4.telnet 服务器ip 端口 看该服务器指定端 ...
- 用户不再sudoers文件中
1.修改/etc/sudoers文件权限 # chmod 777 /etc/sudoers 2.编辑/etc/sudoers文件,添加要提升权限的用户: 在文件中找到root ALL=(ALL) AL ...
- CIDR概述及其地址块计算
CIDR概述 英文:Classless Inter-Domain Routing,中文是:无分类域间路由选择.一般叫做无分类编址. 设计目的:解决路由表项目过多过大的问题. 表示法:{<网络前缀 ...
- windows Server 2012/2016 路由和远程访问,PPPOE,ADSL,连接接口时出现一个错误,连接被远程计算机终止
经过查询资料,是由mprddm.dll的bug引起的. 修改位置: 将je修改为jmp. 查找修改位置,可参考 前面的RasGetPortUserData的调用,或者 后面的 字符串 64位dll可使 ...
- SQLServer 删除表中的重复数据
create table Student( ID varchar(10) not null, Name varchar(10) not null, ); insert in ...