【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的更多相关文章

  1. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

  2. SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】

    LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...

  3. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  4. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  5. spoj1812 LCS2 - Longest Common Substring II

    地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags  A string is fi ...

  6. 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 ...

  7. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  8. 题解 SP1812 【LCS2 - Longest Common Substring II 】

    对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...

  9. 【刷题】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 ...

随机推荐

  1. ElementUI制作树形表组件

    提要 最近项目中需要用到树形表格来描述部门.区域之间的父子展开关系.但是已经在项目中使用的Vue的成熟组件ElementUI以及iViewUI组件都没有提供相应的树形表格组件,无奈找了其他替代方案也都 ...

  2. LeetCode题解之N-ary Tree Postorder Traversal

    1.题目描述 2.问题分析 递归. 3.代码 vector<int> postorder(Node* root) { vector<int> v; postNorder(roo ...

  3. 使用python快速搭建本地网站

    如果你急需一个简单的Web Server,但你又不想去下载并安装那些复杂的HTTP服务程序,比如:Apache,ISS,Nodejs等.那么, Python 可能帮助你.使用Python可以完成一个简 ...

  4. Gradle 'MYasprj' project refresh failed Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容

    Gradle ‘MYasprj’ project refresh failed Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容.请 ...

  5. CSS未知宽高元素水平垂直居中

    方法一 :table.cell-table 思路:显示设置父元素为:table,子元素为:cell-table,这样就可以使用vertical-align: center,实现水平居中优点:父元素(p ...

  6. TCP是如何实现三次握手的?

    什么是三次握手 TCP是网络传输层的协议,提供面向连接的可靠的字节流服务,要通信得先建立连接 所谓三次握手就是指,建立一个TCP连接时,需要CLient与Server发送三个包,确认连接的建立 这一过 ...

  7. mitmproxy

    通过脚本定制化实现篡改request或者response mitmproxy 顾名思义中间人代理[man-in-the-middle proxy],和fiddler.Charles等工具类似,通过代理 ...

  8. RLP(转发注明出处)

    目录 RLP序列化 什么是序列化? 为什么要序列化? RLP序列化处理的两项数据 RLP序列化采取的5项规则: 利用python写的RLP 实际中的使用是个怎么样子? RLP分析 参考目录 @ RLP ...

  9. 【转】默认网关有什么用?我应当怎么填写默认网关和DNS呢

    默认网关有什么用?我应当怎么填写默认网关和DNS呢? 目前使用的是pppoe方式上网,无猫,只是将一根入户的网线插在无线路由上面,然后在路由中设置ppoe方式上网,输入帐号密码.一般电脑和手机全设成了 ...

  10. Linux备份压缩命令

    gzip 命令 把/home/chenjialins目录下的familyA目录下所有文件压缩成.gz文件cd /home/chenjialinstar -cvf /home/chenjialins/f ...