spoj1811
题解:
后缀自动机
先把A的后缀自动机建好
然后用B再上面跑
如果不能转移就跳fail
如果可以就到下一个可行状态
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=;
char b[N],a[N];
int n,m,cnt,la;
struct State
{
int ch[],fa,len;
void init()
{
fa=-;
len=;
memset(ch,-,sizeof ch);
}
}T[N*];
void Extend(int c)
{
int end=++cnt,tmp=la;
T[end].init();
T[end].len=T[tmp].len+;
while (tmp!=-&&T[tmp].ch[c]==-)
{
T[tmp].ch[c]=end;
tmp=T[tmp].fa;
}
if (tmp==-)T[end].fa=;
else
{
int ne=T[tmp].ch[c];
if (T[tmp].len+==T[ne].len)T[end].fa=ne;
else
{
int np=++cnt;
T[np]=T[ne];
T[np].len=T[tmp].len+;
T[end].fa=T[ne].fa=np;
while (tmp!=-&&T[tmp].ch[c]==ne)
{
T[tmp].ch[c]=np;
tmp=T[tmp].fa;
}
}
}
la=end;
}
void solve()
{
int ans=;
T[].init();
for (int i=;i<=n;i++)Extend(a[i]-'a');
int Lcs=,o=;
for (int i=;i<=m;i++)
{
int c=b[i]-'a';
if (T[o].ch[c]!=-)
{
o=T[o].ch[c];
ans=max(ans,++Lcs);
}
else
{
while (o!=-&&T[o].ch[c]==-)o=T[o].fa;
if (o==-)o=Lcs=;
else
{
Lcs=T[o].len+;
o=T[o].ch[c];
ans=max(ans,Lcs);
}
}
}
printf("%d\n",ans);
}
int main()
{
scanf("%s%s",a+,b+);
n=strlen(a+);
m=strlen(b+);
solve();
return ;
}
spoj1811的更多相关文章
- SPOJ1811 && SPOJ1812
SPOJ1811 && SPOJ1812 LCS && LCS2 非常神奇的两道题... 题目大意: 给定n个字符串,求最长公共子串 做法1: 后缀数组: 把字符串连起 ...
- 【spoj1811 & spoj1812 - LCS1 & LCS2】sam
spoj1811 给两个长度小于100000的字符串 A 和 B,求出他们的最长公共连续子串. 先将串 A 构造为 SAM ,然后用 B 按如下规则去跑自动机.用一个变量 lcs 记录当前的最长公共 ...
- spoj1811 Longest Common Substring
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- SPOJ1811最长公共子串问题(后缀自动机)
题目:http://www.spoj.com/problems/LCS/ 题意:给两个串A和B,求这两个串的最长公共子串. 分析:其实本题用后缀数组的DC3已经能很好的解决,这里我们来说说利用后缀自动 ...
- spoj1811:Longest Common Substrin
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=2796 把一个字符串做出后缀自动机,另一个字符串与之匹配. #include<cs ...
- HUSTOJ 2796 && SPOJ1811
传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2796 题解:后缀自动机,很裸,但是感觉对后缀自动机还不是特别理解,毕竟我太蒟蒻,等我精通 ...
- 后缀自动机模板(SPOJ1811)
用后缀自动机实现求两个串的最长公共子串. #include <cstdio> #include <algorithm> ; char s[N]; ]; int main() { ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
随机推荐
- django数据库的增、删、改、查
1.增加 第一种:save 通过创建模型类对象,执行对象的save()方法保存到数据库中. 第二种:create 2.修改 3.查询 get 查询单一结果,如果不存在会抛出模型类.DoesNotExi ...
- HDU - 1400 Mondriaan's Dream
HDU - 1400 思路: 轮廓线dp入门题 #include<bits/stdc++.h> using namespace std; #define fi first #define ...
- 荧光原位杂交技术 RNA-FiSH (fluorescence in situ hybridization)
通俗理解:带有荧光标记的DNA探针可以用于检测活体内特定基因的表达情况,活体成像. 荧光原位杂交方法是一种物理图谱绘制方法,使用荧光素标记探针,以检测探针和分裂中期的染色体或分裂间期的染色质的杂交.荧 ...
- vs2013安装及opencv3.0的配置
vs2013的安装改善计划,不勾选. Windows8 和 windows phone不勾选 然后进行解压安装.(我安装在了e盘的次级目录) 安装完成,点击“启动” 登陆界面,点击“以后再说”. ...
- WAV和PCM文件转换的程序
using System;using System.IO;using System.Text;using System.Windows.Forms;using System.Runtime.Inter ...
- LeetCode--258--各位相加*
问题描述: 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 ...
- p2739 Shuttle Puzzle
观察样例得知就是和离'_'左边最近的'w'交换位置,然后和离'_'右边最近的'b'交换位置,轮流进行. #include <iostream> #include <cstdio> ...
- android -------- Data Binding的使用(二)
分享一下Data Binding在项目中一些常用的基础,点击事件和输入框的一些操作. DataBinding允许我们在xml中view的一些事件属性(如onClick等)中填写DataBinding表 ...
- Oracl 12c安装
Oracl安装部署 一.前置条件准备 修改hostname: hostname oracle 修改/etc/hosts:添加192.168.10.106 oracle 添加软件开发工具 搭建yum源 ...
- find xss
from :http://blog.sina.com.cn/s/blog_68d342d90100nh7b.html 什么是XSS跨站攻击: http://baike.baidu.com/view/5 ...