题意:

求\(n\)个串的最大\(LCS\)。

思路:

把第一个串建后缀自动机,然后枚举所有串。对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少),然后我们求出最终所有串在\(i\)节点的匹配最小值\(mn[i]\)(即为所有串在\(i\)节点都能取到多少),答案即为\(max\{min[i]\}\)。

但是我们能发现,如果我们更新了\(temp[i]\),那么其实\(fa[i]\)的\(temp[fa[i]]\)也应该要更新,因为父节点是我的后缀子串,只是我没有走过去而已,并且\(temp[fa[i]] = max(temp[fa[i]], \ max(temp[i],\ mxlen[fa[i]]))\),因为父节点的匹配长度不能超过他本身长度。

为了能线性实现如上操作,我们按照\(mxlen\)大小桶排,因为父节点的\(mxlen\)一定小于子节点,那么我直接倒着更新就能保证我更新父节点时自己一定已经更新过了。

黄某讲的挺好,点击前往

代码:

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 10;
typedef long long ll;
const ll MOD = 1e9 + 7;
int x[maxn << 1], rk[maxn << 1];
//桶排,按照len从小到大排序节点
struct SAM{
int node[maxn << 1][26], fa[maxn << 1], mxlen[maxn << 1];
int mn[maxn << 1];
int sz, last;
int newnode(){
++sz;
memset(node[sz], 0, sizeof(node[sz]));
fa[sz] = mxlen[sz] = 0;
return sz;
}
void init(){
sz = 0;
last = newnode();
}
void insert(int k){
int p = last, np = last = newnode();
mxlen[np] = mxlen[p] + 1;
for(; p && !node[p][k]; p = fa[p])
node[p][k] = np;
if(p == 0){
fa[np] = 1;
}
else{
int t = node[p][k];
if(mxlen[t] == mxlen[p] + 1){
fa[np] = t;
}
else{
int nt = newnode();
memcpy(node[nt], node[t], sizeof(node[t]));
fa[nt] = fa[t];
mxlen[nt] = mxlen[p] + 1;
fa[np] = fa[t] = nt;
for(; p && node[p][k] == t; p = fa[p])
node[p][k] = nt;
}
}
}
void Sort(int len){
//桶排,按照len从小到大排序节点
for(int i = 1; i <= sz; i++) x[mxlen[i]]++;
for(int i = 1; i <= len; i++) x[i] += x[i - 1];
for(int i = 1; i <= sz; i++) rk[x[mxlen[i]]--] = i; for(int i = 1; i <= sz; i++) mn[i] = mxlen[i];
}
int tmp[maxn << 1];
void build(char *s){
for(int i = 0; i <= sz; i++) tmp[i] = 0;
int len = strlen(s);
int pos = 1;
int ret = 0;
for(int i = 0; i < len; i++){
int c = s[i] - 'a';
while(pos && node[pos][c] == 0){
pos = fa[pos];
ret = mxlen[pos];
}
if(pos == 0){
ret = 0;
pos = 1;
}
else{
pos = node[pos][c];
ret++;
}
tmp[pos] = max(tmp[pos], ret);
}
for(int i = sz; i >= 1; i--){
int c = rk[i];
tmp[fa[c]] = min(max(tmp[fa[c]], tmp[c]), mxlen[fa[c]]);
}
for(int i = 1; i <= sz; i++){
mn[i] = min(mn[i], tmp[i]);
}
}
void query(){
int ret = 0;
for(int i = 1; i <= sz; i++) ret = max(ret, mn[i]);
printf("%d\n", ret);
} }sam;
char s[maxn];
int main(){
sam.init();
scanf("%s", s);
int len = strlen(s);
for(int i = 0; i < len; i++) sam.insert(s[i] - 'a');
sam.Sort(len);
sam.build(s);
while(~scanf("%s", s)){
sam.build(s);
}
sam.query();
return 0;
}

SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解的更多相关文章

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

  2. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

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

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

  4. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

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

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

  6. SPOJ LCS2 - Longest Common Substring II

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

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

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

  8. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

  9. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

随机推荐

  1. 安装macosx10.13high serria

    本教程所需资源下载链接: 链接:https://pan.baidu.com/s/1wGTezXz6zGvtlwpv6mMoSg 提取码:r6n9 安装VMware workstation 16.0,安 ...

  2. Django-html文件实例

    1.实例1,登陆界面 <!DOCTYPE html> <head> <meta http-equiv="content-type" content=& ...

  3. 订阅者模式,公众号、B站、快手用了都说好!

    大家好,今天和大家来聊一个新的设计模式--订阅者模式. 这个模式在我们的生活当中非常常见,可以说是几乎所有的媒体平台都用或多或少地用到了这个模式.比如公众号,我们来仔细梳理一下公众号这个平台当中的整个 ...

  4. java实现Excel定制导出(基于POI的工具类)

    我的需求: 项目中有一些工程表格需要导出,设计到行列合并,定制样式,原有工具类冗余,内聚性强.所以想写一个可以随意定制excel的工具类,工具类满足需求: 对于常用的工程表格有模板格式,可以任意插拔. ...

  5. GStreamer环境搭建篇

    GStreamer是一套强大的多媒体中间件系统,跟FFmpeg功能类似. 各个Linux发行版(Ubuntu,fedora),大都集成了GStreamer相关工具,而作为软件层次结构最上层的播放器,几 ...

  6. .NET Core部署到linux(CentOS)最全解决方案,入魔篇(使用Docker+Jenkins实现持续集成、自动化部署)

    通过前面三篇: .NET Core部署到linux(CentOS)最全解决方案,常规篇 .NET Core部署到linux(CentOS)最全解决方案,进阶篇(Supervisor+Nginx) .N ...

  7. Linux 技巧:让进程在后台运行更可靠的几种方法

    Linux 技巧:让进程在后台运行更可靠的几种方法 https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/index.html 我们经常会碰到这 ...

  8. linux反弹shell总结

    1.1发送文件(公网发内网) 文件发送端: nc -lp 6666 < 文件 文件接收端: nc 发送端ip 发送端端口 > 新文件 1.2发送文件(内网发公网)文件发送端: nc -lp ...

  9. 39.NFS(网络文件系统)

    要共享文件的主机都是Linux系统时推荐在客户端部署NFS 服务来共享文件. NFS(网络文件系统)服务可以将远程Linux 系统上的文件共享资源挂载到本地主机的目录上,从而使得本地主机(Linux ...

  10. ElasticSearch 入门简介

    公号:码农充电站pro 主页:https://codeshellme.github.io ElasticSearch 是一款强大的.开源的.分布式的搜索与分析引擎,简称 ES,它提供了实时搜索与聚合分 ...