SPOJ 1812 Longest Common Substring II(后缀自动机)
【题目链接】 http://www.spoj.com/problems/LCS2/
【题目大意】
求n个串的最长公共子串
【题解】
对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值,
取最小值中的最大值即可。由于跑的地方只记录了匹配结尾的状态,
所以还需要更新parent树上的状态,既然匹配到了子节点,
那么parent树链上的值就都能够取到l,
一开始给每个不同状态按照l从小到大分配储存地址,
这样,我们就可以从匹配长度最长的开始更新parent树的情况。
【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=500005;
char s[N];
struct sam{
int p,q,np,nq,cnt,last,a[N][26],l[N],f[N],mx[N],x[N];
sam(){cnt=0;last=++cnt;}
void extend(int c){
p=last;np=last=++cnt;l[np]=l[p]+1;
while(!a[p][c]&&p)a[p][c]=np,p=f[p];
if(!p)f[np]=1;
else{
q=a[p][c];
if(l[p]+1==l[q])f[np]=q;
else{
nq=++cnt;l[nq]=l[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
f[nq]=f[q]; f[np]=f[q]=nq;
while(a[p][c]==q)a[p][c]=nq,p=f[p];
}
}
}
void build(){
scanf("%s",s+1);
int len=strlen(s+1);
for(int i=1;i<=len;i++)extend(s[i]-'a');
for(int i=1;i<=cnt;i++)mx[l[i]]++;
for(int i=1;i<=len;i++)mx[i]+=mx[i-1];
for(int i=1;i<=cnt;i++)x[mx[l[i]]--]=i;
for(int i=1;i<=cnt;i++)mx[i]=l[i];
}
void doit(){
int len=strlen(s+1),tmp=0,p=1;
static int arr[N];
for(int i=1;i<=len;i++){
int c=s[i]-'a';
if(a[p][c])p=a[p][c],tmp++;
else{
while(p&&!a[p][c])p=f[p];
if(!p)p=1,tmp=0;
else tmp=l[p]+1,p=a[p][c];
}arr[p]=max(arr[p],tmp);
}for(int i=cnt;i;i--){
int t=x[i];
mx[t]=min(mx[t],arr[t]);
if(arr[t]&&f[t])arr[f[t]]=l[f[t]];
arr[t]=0;
}
}
void getans(){
int ans=0;
for(int i=1;i<=cnt;i++)ans=max(ans,mx[i]);
printf("%d\n",ans);
}
}sam;
int main(){
sam.build();
while(~scanf("%s",s+1))sam.doit();
sam.getans();
return 0;
}
SPOJ 1812 Longest Common Substring II(后缀自动机)的更多相关文章
- 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 ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1812 Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
随机推荐
- javascript正则表达式/g与/i及/gi的意义
regularexpression=/pattern/[switch] 这个switch就有三种值 g: 全局匹配 i: 忽略大小写 gi: 全局匹配 + 忽略大小写 JScript 语言参考 --- ...
- 转:Server.MapPath相关
如果你从Page类继承的类中执行这条语句,才可以简单地使用 DataBase = Server.MapPath("data.mdb");否则写全命名空间:System.Web.Ht ...
- 对原生js的一些小尝试
意图仿造JQ操作以及弄个个人工具箱,不断完善中,代码均为个人摸索,所以肯定会有不少不足的地方,希望读者们能提出来. var xzhUtils = { //-----DOM对象添加类----- //-- ...
- Oracle inner join、left join、right join 、+左边或者右边的区别
我们以Oracle自带的表来做例子 主要两张表:dept.emp 一个是部门,一个是员工表结构如下: emp name null? Type Empno not null number(4) enam ...
- http://webhelp.esri.com/arcgisexplorer/2500/zh-CN/index.html#add_raster_data.htm
http://webhelp.esri.com/arcgisexplorer/2500/zh-CN/index.html#add_raster_data.htm
- Oracle EBS-SQL (SYS-10):锁定表查询.sql
/*死锁查询-1*/ SELECT o.object_name, l.session_id,l.process, l.locked_mode FROM v$locked_object l , dba_ ...
- Top free and open source log management software
As mentioned in the previous post, in my quest to find an alternative to Kiwi Syslog, I looked at a ...
- JAVA中的break[标签]continue[标签]用法
原文:JAVA中的break[标签]continue[标签]用法 注意:JAVA中的标签必须放在循环之前,且中间不能有其他语句.例如:tag:for或while或do--while; 1.使用brea ...
- ultravnc
virsh attach-disk
- 用showModalDialog写的简单弹出框传参与反参
vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures]) sURL -- 必选参数,类型:字符串.用来指定对话框要 ...