LCS2 - Longest Common Substring II

多个字符串找最长公共子串

以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是要记录\(SAM\)的每个状态和当前匹配串匹配的最大值\(maxx\),这个在匹配完一个串之后需要通过\(parent\)树上传最大匹配值,同时要更新一个最小值\(minn\),来表示每个节点和当前已经匹配过的所有串能匹配上的最大值,这个需要每次匹配一个串之后和当前节点的\(maxx\)取\(min\)

//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 2e5+7;
struct SAM{
int len[MAXN],link[MAXN],ch[MAXN][26],tot,last,minn[MAXN],maxx[MAXN],c[MAXN],sa[MAXN];
SAM(){ link[0] = -1; }
void extend(int c){
int np = ++tot, p = last;
minn[np] = MAXN;
len[np] = len[p] + 1;
while(p!=-1 and !ch[p][c]){
ch[p][c] = np;
p = link[p];
}
if(p==-1) link[np] = 0;
else{
int q = ch[p][c];
if(len[p]+1==len[q]) link[np] = q;
else{
int clone = ++tot;
minn[clone] = MAXN;
link[clone] = link[q];
len[clone] = len[p] + 1;
memcpy(ch[clone],ch[q],sizeof(ch[q]));
link[np] = link[q] = clone;
while(p!=-1 and ch[p][c]==q){
ch[p][c] = clone;
p = link[p];
}
}
}
last = np;
}
void Radix_sort(){
for(int i = 1; i <= tot; i++) c[i] = 0;
for(int i = 0; i <= tot; i++) c[len[i]]++;
for(int i = 1; i <= tot; i++) c[i] += c[i-1];
for(int i = tot; i >= 0; i--) sa[c[len[i]]--] = i;
}
void match(char *s){
int u = 0, ls = 0;
for(int i = 0, l = strlen(s); i < l; i++){
int c = s[i] - 'a';
while(u and !ch[u][c]) u = link[u], ls = len[u];
if(ch[u][c]) u = ch[u][c], ls++;
maxx[u] = max(maxx[u],ls);
}
for(int i = tot + 1; i >= 1 and sa[i]; i--){
int u = sa[i];
minn[u] = min(minn[u],maxx[u]);
maxx[link[u]] = max(maxx[link[u]],min(len[link[u]],maxx[u]));
maxx[u] = 0;
}
}
int LCS(){ int ret = 0; for(int i = 0; i <= tot; i++) ret = max(ret,minn[i]); return ret; }
}sam;
char s[MAXN];
int main(){
scanf("%s",s);
for(int i = 0, l = strlen(s); i < l; i++) sam.extend(s[i]-'a');
sam.Radix_sort();
while(scanf("%s",s)!=EOF) sam.match(s);
printf("%d\n",sam.LCS());
return 0;
}

SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】的更多相关文章

  1. spoj1812 LCS2 - Longest Common Substring II

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

  2. 【SPOJ - LCS2】Longest Common Substring II【SAM】

    题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...

  3. SPOJ LCS2 - Longest Common Substring II 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...

  4. SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串

    [传送门:SPOJ1811&BZOJ2946] 简要题意: 给出若干个字符串,求出这些字符串的最长公共子串 题解: 后缀自动机 这两道题的区别只是在于一道给出了字符串个数,一个没给,不过也差不 ...

  5. 【SP1812】LCS2 - Longest Common Substring II

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

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

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

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

  9. LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

随机推荐

  1. 为什么.NET Standard 仍然有意义?

    .NET Standard 是.NET 官方的API规范,可在许多.NET环境中使用.之所以存在,面向.NET Standard 2.0的库提供了最大可能的覆盖范围,并启用了几乎所有现代的.NET功能 ...

  2. 【MyBatis】MyBatis 缓存

    MyBatis 缓存 文章源码 什么是缓存 像大多数的持久化框架一样,MyBatis 也提供了缓存策略,通过缓存策略来减少数据库的查询次数,从而提高性能. Mybatis 中缓存分为一级缓存,二级缓存 ...

  3. Spring Boot超详细用户管理项目(零)——开发前准备

    开始前的软件准备:(编写中:未完成) 使用软件介绍: Java版本:Java SE 11(LTS) 开发工具:IDEA(2020.3版本) Linux系统: 数据库: Java 版本:Java SE ...

  4. Can't locate CPAN.pm in @INC

    [root@test]# perl -MCPAN -e 'install DBD::mysql'Can't locate CPAN.pm in @INC (@INC contains: /usr/lo ...

  5. 【Linux】同时插入多行数据到文本文件中

    如果想同时插入多行数据到指定的文本中,可以用下面的命令EOF cat >> test.txt <<EOF 1234 5678 GOOD EOF 将上述3行插入到 test.tx ...

  6. 【网络】trunk和vlan配置

    篇一 : trunk配置和vlan配置 trunk配置 Switch>enable ? ? ?//进入特权模式 Switch#conf t ? ? ?//进入配置模式 Switch(config ...

  7. Django实现文件在本地的存储和读取

    需求介绍:将图片存入本地的电脑文件夹中,将图片的路径保存到数据库,然后通过数据库的路径读取文件: 1.文件的存入: 前端文件: <form class="form-horizontal ...

  8. django url别名和反向解析 命名空间

    url别名和反向解析 我们平时写的url名字都是死的,如果项目过大,需要项目中某个文件名改动一下,那么改动起来就不是一般的麻烦了,所以我们就在定义的时候给url起一个别名,以后不管哪个文件中运用都是用 ...

  9. 干货!上古神器 sed 教程详解,小白也能看的懂

    目录: 介绍工作原理正则表达式基本语法数字定址和正则定址基本子命令实战练习 介绍 熟悉 Linux 的同学一定知道大名鼎鼎的 Linux 三剑客,它们是 grep.awk.sed,我们今天要聊的主角就 ...

  10. 关于Vue v-model你需要知道的一切

    ​v-model是Vue的一个指令,它提供了input和form数据之间或两个组件之间的双向数据绑定. 这在Vue开发中是一个简单的概念,但是v-model的真正威力需要一些时间才能理解. 到本教程结 ...