【codevs3160】 LCS 【后缀自动机】
题意
给出两个字符串,求它们的最长公共子串。
分析
后缀自动机的基础应用。
比如两个字符串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 【后缀自动机】的更多相关文章
- POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...
- SPOJ - LCS 后缀自动机入门
LCS - Longest Common Substring A string is finite sequence of characters over a non-empty finite set ...
- SPOJ LCS 后缀自动机
用后缀自动机求两个长串的最长公共子串,效果拔群.多样例的时候memset要去掉. 解题思路就是跟CLJ的一模一样啦. #pragma warning(disable:4996) #include< ...
- SPOJ LCS 后缀自动机找最大公共子串
这里用第一个字符串构建完成后缀自动机以后 不断用第二个字符串从左往右沿着后缀自动机往前走,如能找到,那么当前匹配配数加1 如果找不到,那么就不断沿着后缀树不断往前找到所能匹配到当前字符的最大长度,然后 ...
- SPOJ 1811 LCS [后缀自动机]
题意: 求两个串的最大连续子串 一个串建SAM,另一个串在上面跑 注意如果走了Suffix Link,sum需要更新为t[u].val+1 Suffix Link有点像失配吧,当前状态s走不了了就到S ...
- 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, \(\ ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
随机推荐
- openresty luarocks 安装以及openssl 问题处理
1. 安装方式 wget https://luarocks.github.io/luarocks/releases/luarocks-2.4.3.tar.gz tar -xzvf luarocks ...
- 在使用windows调用Hadoop 错误 /bin/bash: line 0: fg: no job control一般解决方法
在使用windows调用Hadoop yarn平台的时候,一般都会遇到如下的错误: 2014-05-28 17:32:19,761 WARN org.apache.hadoop.yarn.server ...
- c#代码加密
源代码保护:怎样利用MaxtoCode加密dotNet源代码 http://www.webkaka.com/blog/archives/MaxtoCode-encrypt-dotnet-program ...
- bower.json 的版本范围
bower.json 的版本范围 有小伙伴问 ~2.2.0 什么意思. 而且在git 的tags 中没有了 2.2.0 版本,怎么样? 实际上 ~2.2.0 的意思是 >=2.2.0 <2 ...
- Linux 文件系统目录结构
进入 Linux 根目录(即 "/",Linux文件系统的入口,也是处于最高一级的目录),运行 "ls -l" 命令,可以看到 Linux 系统目录. 1./b ...
- C#如何:启用和禁用自动绑定重定向 (微软)
https://msdn.microsoft.com/zh-cn/library/2fc472t2.aspx 如何:启用和禁用自动绑定重定向 .NET Framework (current versi ...
- EasyUI 左,右(上、下)布局
左,右(上.下)布局 <body class="easyui-layout"> <div data-options="region:'west',col ...
- django随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: 1 python3.5 -m pip install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from P ...
- Git学习之常用的命令
配置git git config --global user.name "你的github用户名" git config --global user.email "你的G ...
- 03_java之基本语法
01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型. * b: 引用数据类型的定义格式 * 与定义基本数据类型变 ...