题意

给出两个字符串,求它们的最长公共子串。

分析

后缀自动机的基础应用。

比如两个字符串s1和s2,我们把s1建为SAM,然后根据s2跑,找出s2每个前缀的最长公共后缀。

我们可以理解为,当向尾部增加一个字母的时候,就按照匹配路径来走,当在SAM中找不到这样的字符串的时候,就要减少头部的字母,就顺着parent树往祖先结点走。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map> using namespace std;
const int maxn=1e5+;
char S[maxn],T[maxn];
int n,m;
struct state{
int len,link;
map<char,int>next;
}st[*maxn];
int last,cur,sz;
void init(){
last=cur=;
sz=;
st[].len=;
st[].link=-;
// st[0].next.clear();
}
void build_sam(char c){
cur=sz++;
st[cur].len=st[last].len+;
int p;
for(p=last;p!=-&&!st[p].next.count(c);p=st[p].link){
st[p].next[c]=cur;
}
if(p==-)
st[cur].link=;
else{
int q=st[p].next[c];
if(st[p].len+==st[q].len){
st[cur].link=q;
}else{
int clone=sz++;
st[clone].len=st[p].len+;
st[clone].next=st[q].next;
st[clone].link=st[q].link;
for(;p!=-&&st[p].next[c]==q;p=st[p].link){
st[p].next[c]=clone;
}
st[cur].link=st[q].link=clone;
}
}
last=cur;
} int main(){
scanf("%s%s",S,T);
n=strlen(S);
m=strlen(T);
init();
for(int i=;i<n;i++){
build_sam(S[i]);
}
int cur=;
int v=,len=,ans=;
for(int i=;i<m;i++){
if(st[v].next.count(T[i])){
v=st[v].next[T[i]];
len++;
}else{
while(v!=-&&!st[v].next.count(T[i])){
// printf("%d\n",v);
v=st[v].link;
len=st[v].len;
}
if(v==-)
v=len=;
else{
v=st[v].next[T[i]];
len++;
}
}
ans=max(ans,len);
}
printf("%d\n",ans); return ;
}

【codevs3160】 LCS 【后缀自动机】的更多相关文章

  1. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

  2. SPOJ - LCS 后缀自动机入门

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

  3. SPOJ LCS 后缀自动机

    用后缀自动机求两个长串的最长公共子串,效果拔群.多样例的时候memset要去掉. 解题思路就是跟CLJ的一模一样啦. #pragma warning(disable:4996) #include< ...

  4. SPOJ LCS 后缀自动机找最大公共子串

    这里用第一个字符串构建完成后缀自动机以后 不断用第二个字符串从左往右沿着后缀自动机往前走,如能找到,那么当前匹配配数加1 如果找不到,那么就不断沿着后缀树不断往前找到所能匹配到当前字符的最大长度,然后 ...

  5. SPOJ 1811 LCS [后缀自动机]

    题意: 求两个串的最大连续子串 一个串建SAM,另一个串在上面跑 注意如果走了Suffix Link,sum需要更新为t[u].val+1 Suffix Link有点像失配吧,当前状态s走不了了就到S ...

  6. 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, \(\ ...

  7. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

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

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

  9. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

随机推荐

  1. 查找大文件 & 索引节点(inode)爆满 解决办法

    经常遇到磁盘满或者文件节点满的情况,整理如下 查找大文件 查找超过某个大小的文件, 如1G find . -type f -size +1G 查找文件大小的时候,现实文件属性 find . -type ...

  2. consul 几个方便使用的类库

    consul 几个方便使用的类库 1. java  https://github.com/OrbitzWorldwide/consul-client   <dependency> < ...

  3. distinct和group by的性能比较

    distinct和group by的性能比较 当去重复的字段 的个数比较多的时候,group by 比distinct要快很多 当去重复的字符 的个数比较少的时候,distinct 比group by ...

  4. flash无法导入mp3文件

    用全能音频转换通转换下,一切采用默认即可,点击下载地址 这个软件真心好用. 把mp3拖入其中,点击批量转换,参数默认,即可.

  5. sphinx-1.3.0扩展在pPHP 7.0.7版本编译不通过

    在这个网友也是在php7上面编译插件不通过 https://bugs.php.net/bug.php?id=71586 下php7对应的扩展文件即可 http://git.php.net/?p=pec ...

  6. opencv Mat中某点的值

    Mat mat = imread("baby.jpg"); Mat p = mat.col().row(); uchar* ptr = (uchar*) p.data; ]; ]; ...

  7. Java-Runoob:Java 数组

    ylbtech-Java-Runoob:Java 数组 1.返回顶部 1. Java 数组 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java 语言 ...

  8. CentOS6.4 上搭建NIS网络信息服务器

    NIS(Network Information Service)网络信息服务,主要功能是提供用户登录信息给客户端主机查询之用,用于企业局域网Linux主机账户的集中管理(非跨平台).NIS服务器在大型 ...

  9. hadoop中的一次集群任务执行超时问题查找过程

    问题背景 本次进行一个项目的重构,在某些活动数据量比较大的情况下,会偶尔出现1200s超时的情况,如下: AttemptID:attempt_1410771599055_11709_m_000033_ ...

  10. http 和 https 区别?

    1. HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头2. HTTP 是不安全的,而 HTTPS 是安全的3. HTTP 标准端口是80 ,而 HTTPS ...