【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. c#中partial 作用

    申明一下:我也是在百度上找的答案,然后合起来的,这样感觉好理解一点!partial是局部类型的意思就是说有这个关键字的类.结构或接口可以写成几个部分比如: public partial class P ...

  2. Sql Server中的游标最好只用于有主键或唯一键的表

    游标cursor,我想大多数人都在sql server里面用过.当一个表数据量不太大的时候,游标还是可以用的,毕竟游标是循环一个表中每一行数据的最简便办法.但是如果你用一个游标去循环一个没有主键或唯一 ...

  3. mysql瑞士军刀–pt工具

    Percona-Toolkits Percona-toolkit 简介 percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql任务和系统任务,这些 ...

  4. 通过yum源在centOS7安装mysql8

    1.去官网下载rpm文件,该文件专门用于yum安装方式: 到官网https://www.mysql.com/downloads/下载社区版Community(针对个人),如下图: 然后拉到最下面,我下 ...

  5. MySQL核心之双一原则

    所谓的双一就是指: sync_binlog=; innodb_flush_log_at_trx_commit= innodb_flush_log_at_trx_commit和sync_binlog这两 ...

  6. Hadoop HBase概念学习系列之HMaster服务器(四)

    每台HRegion服务器都会和HMaster服务器通信,HMaster的主要任务就是告诉每个HRegion服务器它要维护哪些HRegion. 当一台新的HRegion服务器登录到HMaster服务器时 ...

  7. vc获取当前进程CPU使用率

    double GetCPUUserRate() { HANDLE hProcess=::GetCurrentProcess(); static DWORD s_dwTickCountOld = 0; ...

  8. css3自定义滚动条背景透明

    .editor{ overflow:hidden; height:640px; padding:0 45px; border: 0 none; outline: none; } .editor::-w ...

  9. 用python实现MRO算法

    引子: 如图反映了python3中,几个类的继承关系和查找顺序.对于类A,其查找顺序为:A,B,E,C,F,D,G,(Object),这并不是一个简单的深度优先或广度优先的规律.那么这个顺序到底是如何 ...

  10. Netty入门(十)解码分隔符和基于长度的协议

    我们需要区分不同帧的首尾,通常需要在结尾设定特定分隔符或者在首部添加长度字段,分别称为分隔符协议和基于长度的协议,本节讲解 Netty 如何解码这些协议. 一.分隔符协议 Netty 附带的解码器可以 ...