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, \(\sum\) is the set of lowercase letters.
Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task is a bit harder, for some given strings, find the length of the longest common substring of them.
Here common substring means a substring of two or more strings.
Input
The input contains at most 10 lines, each line consists of no more than 100000 lowercase letters, representing a string.
Output
The length of the longest common substring. If such string doesn't exist, print "0" instead.
Example
Input:
alsdfkjfjkdsal
fdjskalajfkdsla
aaaajfaaaa
Output:
2
Notice: new testcases added
题意:
给出多个字符串,求多个串的最长公共子串
题解:
把第一个串建一个后缀自动机,之后把每个串都在上面跑一个\(LCS\)如这个,跑的时候记录每一个点的最大匹配长度。每跑完一个串把当前答案在\(parent\)树上反向拓扑一下,更新每个点在所有答案中的最小答案。最后再取个最大值就行了。
#include<bits/stdc++.h>
using namespace std;
const int N=200010;
char s[N];
int a[N],c[N];
void cmax(int &a,int b){
a=max(a,b);
}
void cmin(int &a,int b){
a=min(a,b);
}
struct SAM{
int last,cnt;
int size[N],ch[N][26],fa[N<<1],l[N<<1],mx[N<<1],mn[N<<1];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
size[np]=1;
}
void build(char s[]){
memset(mn,0x3f,sizeof mn);
int len=strlen(s+1);
last=cnt=1;
for(int i=1;i<=len;++i)ins(s[i]-'a');
}
void calc(){
for(int i=1;i<=cnt;++i)c[l[i]]++;
for(int i=1;i<=cnt;++i)c[i]+=c[i-1];
for(int i=1;i<=cnt;++i)a[c[l[i]]--]=i;
}
void work(char s[]){
int len=strlen(s+1);
int p=1,left=0,as=0;
while(left<=len){
left++;
while(p&&(!ch[p][s[left]-'a']))p=fa[p],as=l[p];
if(!p)p=1,as=0;
else{
as++;
p=ch[p][s[left]-'a'];
cmax(mx[p],as);
}
}
for(int i=cnt;i;--i){
int p=a[i],f=fa[p];
cmax(mx[f],min(mx[p],l[f]));
cmin(mn[p],mx[p]);mx[p]=0;
}
}
void tj(){
int ot=0;
for(int i=1;i<=cnt;++i)
cmax(ot,mn[i]);
cout<<ot<<endl;
}
}sam;
int main(){
cin>>s+1;
sam.build(s);int js=0,ll=strlen(s+1);
sam.calc();
while(cin>>s+1)
sam.work(s);
sam.tj();
}
LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)的更多相关文章
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 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 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 【刷题】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 ...
- 题解 SP1812 【LCS2 - Longest Common Substring II 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
随机推荐
- macos安装postman
安装命令 brew cask install postman brew 是从下载源码解压然后./configure && make install,同时会包含相关依存库.并自动配置 好 ...
- 无法启动MYSQL服务”1067 进程意外终止”解决的方法——汇总及终极方法
自己一開始依照百度经验里的方法--<MySQL下载安装.配置与使用(win7x64)>去安装和配置,可是到后面步骤总是出现1067代号的错误. 慢慢折腾去解决. 这里汇总各种导致mysql ...
- 注册带有Portal功能的DYN365_ENTERPRISE_PLAN1地址
使用官方进入的注册页面注册后试用,发现没有Portal功能. https://trials.dynamics.com/Dynamics365/Signup 使用以下的地址注册可以产生Portal ht ...
- PAT 1072 开学寄语(20)(代码+思路)
1072 开学寄语(20 分) 下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使其百无聊赖,然后,净面.理发.整衣, ...
- Luogu 3953[NOIP2017] 逛公园 堆优化dijkstra + 记忆化搜索
题解 首先肯定是要求出单源最短路的,我用了堆优化dijikstra ,复杂度 mlogm,值得拥有!(只不过我在定义优先队列时把greater 打成了 less调了好久 然后我们就求出了$i$到源点的 ...
- 深入浅出 JMS(一) - JMS 基本概念
深入浅出 JMS(一) - JMS 基本概念 一.JMS 是个什么鬼 JMS 是 Java Message Service 的简称,即 Java 消息服务.什么是消息服务呢,我们来看一下 Oracle ...
- 检查Makefile中的tab
转:http://stackoverflow.com/questions/16931770/makefile4-missing-separator-stop makefile has a very s ...
- part1:5Linux命令详解
1.Linux命令介绍 Linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.磁盘驱动器.键盘.鼠标还是用户等都是文件.Linux系统管理的命令是它正常运行的 ...
- part1:4-linux快速体验
1.Linux部分目录结构介绍 /:根目录,一般根目录下只存放目录,尽量不要存放文件:/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中. /bin:可执行二进制文件目录, ...
- [GO]kafka的生产者和消费者
生产者: package main import ( "github.com/Shopify/sarama" "fmt" "time" ) ...