SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
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】的更多相关文章
- 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【SAM】
题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串
[传送门:SPOJ1811&BZOJ2946] 简要题意: 给出若干个字符串,求出这些字符串的最长公共子串 题解: 后缀自动机 这两道题的区别只是在于一道给出了字符串个数,一个没给,不过也差不 ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 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 ...
- 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, \(\ ...
随机推荐
- linux常用命令--转载
转载自: https://www.cnblogs.com/Qsunshine/p/10402179.html 常用指令 ls 显示文件或目录 -l列出文件详细信息l(list) -a列出当前目录下所有 ...
- 基于 MPI 的快速排序算法的实现
完整代码: #include <iostream> #include <cstdlib> #include <ctime> #include <algorit ...
- alter column和modify column
5.6中,发现其实alter column 和更改modify column 步骤是一样的 mysql> create table xs(name varchar(12),age int def ...
- Netty学习:伪共享
目录 Netty中的伪共享 伪共享的原理以及介绍 Netty中的伪共享 先说为什么知道这个概念吧,期初看Netty源码的时候,看到了NioEventLoop的构建,其中有这么一句代码: private ...
- 命令模式与go-redis command设计
目录 一.什么是命令(Command)模式 二.go-redis command相关代码 三.总结 一.什么是命令(Command)模式 命令模式是行为型设计模式的一种,其目的是将一个请求封装为一个对 ...
- Centos7下安装MySQL8.0.23-小白的开始
首先简单介绍一下什么叫MySQL: 数据库简而言之就是存储数据的仓库,为了方便数据的存储和管理,它将数据按照特定的规律存储在磁盘上.是为了实现一定的目的,按照某种规则组织起来的数据的集合: MySQL ...
- Python数据模型与Python对象模型
数据模型==对象模型 Python官方文档说法是"Python数据模型",大多数Python书籍作者说法是"Python对象模型",它们是一个意思,表示&quo ...
- linux登陆欢迎信息及命令提示符修改
登录信息修改 登陆信息显示数据 : /etc/issue and /etc/motd 登陆终端机的时候,会有几行提示的字符串,这些设置在/etc/issue里面可以修改,提示内容在/etc/motd中 ...
- linux GPU上多个buffer间的同步 —— ww_mutex、dma-fence的使用 笔记
原文链接:https://www.cnblogs.com/yaongtime/p/14111134.html WW-Mutexes 在GPU中一次Render可能会涉及到对多个buffer的引 ...
- widnows2008双网卡双ip不同Ip段
机房内有不同段ip,因为线路不一样,比如普通带宽和cn2带宽,现有需求配置双网卡双ip ip1: 121.7*.*.* 255.255.255.192 121.7*.*129 ip2: 103.11 ...