后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring
A string is finite sequence of characters over a non-empty finite set Σ.
In this problem, Σ 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 simple, for two 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 exactly two lines, each line consists of no more than 250000 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 Output:
3 这题就是求最长公共子串,于是我直接套用了SAM。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
struct SAM{
int fa[MAXN],ch[MAXN][],len[MAXN],cnt,last;
void Init()
{
last=cnt=;
}
void Insert(int c)
{
int p=last,np=last=++cnt;
len[np]=len[p]+;
while(!ch[p][c]&&p)
ch[p][c]=np,p=fa[p];
if(!p)fa[np]=;
else{
int q=ch[p][c];
if(len[q]==len[p]+)
fa[np]=q;
else{
int nq=++cnt;len[nq]=len[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[p]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
while(ch[p][c]==q)
ch[p][c]=nq,p=fa[p];
}
}
}
}sam;
char str1[MAXN],str2[MAXN];
int main()
{
int len1=,len2=,ans=;
scanf("%s%s",str1,str2);
sam.Init();
while(str1[len1])
sam.Insert(str1[len1++]-'`');
int node=,tmp=;
for(;str2[len2];len2++){
int c=str2[len2]-'`';
if(sam.ch[node][c])
node=sam.ch[node][c],tmp++;
else{
while(node&&!sam.ch[node][c])
node=sam.fa[node];
if(node==)
node=,tmp=;
else
tmp=sam.len[node]+,node=sam.ch[node][c];
}
ans=max(ans,tmp);
}
printf("%d\n",ans);
return ;
}
后缀自动机(SAM) :SPOJ LCS - Longest Common Substring的更多相关文章
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
- SPOJ LCS - Longest Common Substring 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ...
- 【SPOJ】Longest Common Substring
[SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
随机推荐
- 【转载】Shared Configuration
Introduction The Internet changes the ways in which companies handle their day-to-day business and h ...
- Mysql表复制及备份还原
1.复制表结构 1.1 含有主键等信息的完整表结构 CREATE table 新表名 LIKE book; 1.2 只有表结构,没有主键等信息 create table 新表名 s ...
- java simple check whether a file or directory.
Ref: check whether a file or directory First, make sure the path exists by using: new File(path).ex ...
- string.Join和Reverse的简单使用示例
String.Join 方法 (String, String[]) 串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符. 例如,如果 separator 为“,”且 value 的元素为“a ...
- Solaris用户管理(一):用户与组管理
Solaris用户管理(一):用户与组管理 2008-07-01 09:19 用户管理是系统管理的基础.Solaris中不但支持传统Unix所支持的用户和组的概念,还从Solaris 8开始引入了基 ...
- 内联式css样式,直接写在现有的HTML标签中
CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式. 内联式css样式表就是把css代码直接写在现有的HTML标签中 ...
- 刚接触js不久,自己写的banner幻灯片效果。
对于我这种菜鸟来讲,刚接触项目.叫我用插件,其实我说插件太臃肿不想用,倒不如说我是看不懂那些插件...- -(更愿意自己写点看得懂的代码,顺便也是个学习的过程) 所以自己花了些时间,自己来写了个dem ...
- JS自执行函数的几种写法
一:整体写在一个括号中 代码如下: (function Show(){alert("hello");}()) 二:function函数整体外加括号 代码如下: (function ...
- 【随记】解决:VS2010 调试器无法继续继续运行该进程,无法启动调试
今天在调试项目的时候突然出现错误: 按照网上的一些方法弄了后还是同样报错,把本地代码删除后从库上重现拉下来的项目依然报错,到这里就明白不是项目本身问题了,而是VS2010 的问题,经过网上查资料,问同 ...
- js控制 点一下增加一个输入框,点一下增加一个输入框……
<div> <div> 附件1:<input type="file" id="file1" name="file1&qu ...