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\) 每个串依次进行匹配 同时记 ...
随机推荐
- Python进阶之自定义排序函数sorted()
sorted() .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",& ...
- Python进阶--GUI编程
一.图形用户图面(GUI编程) 1. wxpython下载和安装: 下载url: http://wxpython.org/download.php 2.创建示例GUI应用程序 : ①开始需要导入wx ...
- DRAM与NAND Flash产业六大趋势预测分析
集邦科技(TrendForce)旗下的分析部门DRAMeXchange的研究,针对对DRAM与NANDFlash产业的长久观察下,提出了对2012-2015年间产业发展的六大趋势预测: 趋势一 ...
- 如何在C++中获得完整的类型名称(RTTI的typeid在不同平台下有不同的输出值表达,自建类改进了RTTI丢失的信息)
Wrote by mutouyun. (http://darkc.at/cxx-get-the-name-of-the-given-type/) 地球人都知道C++里有一个typeid操作符可以用 ...
- 2016 Multi-University Training Contest 2 总结
第二次多校,出师未捷身先死 欣君看了一下09题,高呼水题,迅速码好,一A. 我看了11题,发现分奇偶讨论即可,于是按思路写好,一A. 欣君搞鼓出01题的一个公式,于是我照着写,一WA.简直不可思议,发 ...
- Day3_字符串操作与正则表达式
本节课的主要内容有:字符串的格式化.连接与分割.比较.匹配和替换.使用正则表达式 字符串的格式化: 去除空格:trim() 使用html格式化:nl2br() 替换‘\n’为‘<br /> ...
- java多线程向数据库写入数据
任务: 从sqlserver中将一个表A(约16W条数据)导到mysql中对应的一个表B中. 思路:分段获取A表中的数据后,用多个线程同时向B表中写入. 关键代码 //将数据库中的数据条数分段 pub ...
- python-认识Socket[入门篇]
什么是socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链 ...
- JS判断是否安装flash player及当前版本
function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(document ...
- Top 10 Mapping APIs: Google Maps, Microsoft Bing Maps and MapQuest
http://www.programmableweb.com/news/top-10-mapping-apis-google-maps-microsoft-bing-maps-and-mapquest ...