题面

给定一些字符串,求出它们的最长公共子串 输入格式 输入至多 \(10\) 行,每行包含不超过 \(100000\)个的小写字母,表示一个字符串 输出格式 一个数,最长公共子串的长度 若不存在最长公共子串,请输出 \(0\)

Sol

一个串建立\(sam\)

每个串在上面匹配

每个点匹配的长度可以由后继转移过来

拓扑序上\(DP\)

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll; template <class Int>
IL void Input(RG Int &x){
RG int z = 1; RG char c = getchar(); x = 0;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= z;
} const int maxn(2e5 + 5); int n, trans[26][maxn], fa[maxn], len[maxn], tot = 1, last = 1, ans, f[maxn], g[maxn];
int id[maxn], t[maxn];
char s[maxn]; IL void Extend(RG int c){
RG int p = last, np = ++tot; last = np;
len[np] = len[p] + 1;
while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
if(!p) fa[np] = 1;
else{
RG int q = trans[c][p];
if(len[q] == len[p] + 1) fa[np] = q;
else{
RG int nq = ++tot;
fa[nq] = fa[q], len[nq] = len[p] + 1;
for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
fa[q] = fa[np] = nq;
while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
}
}
} int main(RG int argc, RG char* argv[]){
scanf(" %s", s), n = strlen(s);
for(RG int i = 0; i < n; ++i) Extend(s[i] - 'a');
for(RG int i = 1; i <= tot; ++i) ++t[g[i] = len[i]];
for(RG int i = 1; i <= tot; ++i) t[i] += t[i - 1];
for(RG int i = 1; i <= tot; ++i) id[t[len[i]]--] = i;
while(scanf(" %s", s) != EOF){
n = strlen(s);
for(RG int i = 1; i <= tot; ++i) f[i] = 0;
for(RG int i = 0, nw = 1, cnt = 0; i < n; ++i){
RG int c = s[i] - 'a';
if(trans[c][nw]) ++cnt, nw = trans[c][nw];
else{
while(nw && !trans[c][nw]) nw = fa[nw], cnt = len[nw];
if(!nw) nw = 1, cnt = 0;
else cnt++, nw = trans[c][nw];
}
f[nw] = max(f[nw], cnt);
}
for(RG int i = tot; i; --i) f[fa[id[i]]] = max(f[fa[id[i]]], f[id[i]]);
for(RG int i = 1; i <= tot; ++i) g[i] = min(g[i], f[i]);
}
for(RG int i = 1; i <= tot; ++i) ans = max(ans, g[i]);
printf("%d\n", ans);
return 0;
}

SPOJ:LCS2 - Longest Common Substring II的更多相关文章

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

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

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

  3. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

  4. SPOJ 1812 LCS2 - Longest Common Substring II

    思路 后缀自动机求多串的最长公共子串 对第一个建出后缀自动机,其他的在SAM上匹配,更新到一个节点的匹配长度最大值即可,最后对所有最大值取min得到一个节点的答案,对所有节点答案求max即可 然后注意 ...

  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 LCS2 - Longest Common Substring II

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

  8. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

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

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

随机推荐

  1. 机器学习笔记(四)--sklearn数据集

    sklearn数据集 (一)机器学习的一般数据集会划分为两个部分 训练数据:用于训练,构建模型. 测试数据:在模型检验时使用,用于评估模型是否有效. 划分数据的API:sklearn.model_se ...

  2. 核心API的使用(给定一个字符串,统计每个字符出现的次数)

    /** * 给定一个字符串,统计每个字符出现的次数. 如:abdaewrwqask435a1aasd */public class ReplaceString { static int length; ...

  3. 学习前端页面css定位

    css元素框定位 一.相对定位: 相对定位是一个非常容易掌握的概念.如果对一个元素进行相对定位,它将出现在它所在的位置上.然后,可以通过设置垂直或水平位置,让这个元素“相对于”它的起点进行移动.pos ...

  4. spring与shiro配置详解

    1.加入shiro相关依赖的jar包 pom.xml部分内容如下: <dependency> <groupId>org.apache.shiro</groupId> ...

  5. i2c_smbs 函数

    i2c_smbus系列函数有: s32 i2c_smbus_read_byte(const struct i2c_client *client); s32 i2c_smbus_write_byte(c ...

  6. es6学习 1

    块级作用域绑定 一 var 声明及变量提升(Hoisting)机制 在函数作用域或全局作用域中通过 var 声明的变量,无论实际上是在哪里声明的,都会被当成在当前作用域顶部声明的变量,这就是我们常说的 ...

  7. Ubuntu18.04配制阿里巴巴的源

    配制阿里巴巴的源步骤 使用阿里巴巴的开源镜像:https://opsx.alibaba.com/mirror 然后选择ubuntu的帮助选项,复制ubuntu18.04镜像源 设置root账户密码: ...

  8. git已经删除了远程分支,本地仍然能看到

    1.使用 git branch -a 命令可以查看所有本地分支和远程分支,发现很多在远程仓库已经删除的分支在本地依然可以看到. 2.使用命令 git remote show origin,可以查看re ...

  9. 我也学习JAVA多线程-join

    在工作中,挺少遇到join关键字,但很多多线程资料和面试过程中,初中级开发工程师总会遇到join. 今天一起学习下join. join的作用:等待指定的时间(当为0时,一直等待),直到这个线程执行结束 ...

  10. (转)C# 正则表达式

    最近写爬虫时需要用到正则表达式,有段时间没有使用正则表达式现在渐渐感觉有些淡忘,现在使用还需要去查询一些资料.为了避免以后这样的情况,在此记录下正则表达式的一些基本使用方法附带小的实例.让以后在使用时 ...