后缀自动机(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 ...
随机推荐
- 9.28noip模拟试题
1.栅栏迷宫 田野上搭建了一个黄金大神专用的栅栏围成的迷宫.幸运的是,在迷宫的边界上留出了两段栅栏作为迷宫的出口.更幸运的是,所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意一点找到一条走出迷宫 ...
- Asp.net中向前端输出JS的一些调用
最近突然写ASP.NET项目,用到向前台输出JS脚本,但是以前在MVC里是通过异步或者一些方法来调用,但是ASP.net用到的很少.在网上找到一个HELPER.CS.保存一下,以后再用. using ...
- (转)Smarty Foreach 使用说明
foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案). foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是 ...
- Log4j简单配置
Log4j是一组强大的日志组件,在项目中时常需要用它提供一些信息,这两天学习了一下它的简单配置. 第一步,我们需要导入log4j-1.2.14.jar到lib目录下 第二步,在src下建立log4j. ...
- ado.net与各种orm操作数据方式的比较
ADO.NET与ORM的比较(1):ADO.NET实现CRUD http://zhoufoxcn.blog.51cto.com/792419/283952 ADO.NET与ORM的比较(2):NHib ...
- laravel4通过控制视图模板路劲来动态切换主题
通过控制视图模板路劲来动态切换主题 App::before(function($request) { $paths = Terminal::isMobile() ? array(__dir__.'/v ...
- TextView控件
1.手动创建(不建议): TextView tv = new TextView(this); tv.setContent("你好"); setContentView(tv); 2. ...
- C# winform 右下角弹窗
[DllImport("user32")] private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, in ...
- 在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步
在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步 下载安装 vscode-ftp-sync 插件. 安装方法1. Ctrl+Shift+P 输入 ext install [插件 ...
- Joomla插件汉化小程序
这两天在搞joomla插件,在看peter的视频,在此谢过他了.看到它汉化插件那个视频.反正闲着无聊,就写了一个Java小程序,方便使用joomla的人汉化插件.这个程序的方法很简单,你只要先运行ou ...