题解:

后缀自动机

先把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的更多相关文章

  1. SPOJ1811 && SPOJ1812

    SPOJ1811 && SPOJ1812 LCS && LCS2 非常神奇的两道题... 题目大意: 给定n个字符串,求最长公共子串 做法1: 后缀数组: 把字符串连起 ...

  2. 【spoj1811 & spoj1812 - LCS1 & LCS2】sam

    spoj1811  给两个长度小于100000的字符串 A 和 B,求出他们的最长公共连续子串. 先将串 A 构造为 SAM ,然后用 B 按如下规则去跑自动机.用一个变量 lcs 记录当前的最长公共 ...

  3. spoj1811 Longest Common Substring

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. SPOJ1811最长公共子串问题(后缀自动机)

    题目:http://www.spoj.com/problems/LCS/ 题意:给两个串A和B,求这两个串的最长公共子串. 分析:其实本题用后缀数组的DC3已经能很好的解决,这里我们来说说利用后缀自动 ...

  5. spoj1811:Longest Common Substrin

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=2796 把一个字符串做出后缀自动机,另一个字符串与之匹配. #include<cs ...

  6. HUSTOJ 2796 && SPOJ1811

    传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2796 题解:后缀自动机,很裸,但是感觉对后缀自动机还不是特别理解,毕竟我太蒟蒻,等我精通 ...

  7. 后缀自动机模板(SPOJ1811)

    用后缀自动机实现求两个串的最长公共子串. #include <cstdio> #include <algorithm> ; char s[N]; ]; int main() { ...

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

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

  9. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

随机推荐

  1. ubuntu 18.04编译opencv3.4.3 with python3.6 cuda9.2 gdal

    惭愧,之前一直没在linux下编译过opencv,也没用过纯命令行版的cmake,现在咬牙编译一次.其实感觉还凑合. opencv官网文档还是那么烂:https://docs.opencv.org/m ...

  2. QTableWidget自定义表头QHeaderView加全选复选框

    1         QTableWidget自定义表头QHeaderView加全选复选框 在使用QTableWidget时需要在表头添加全选复选框,但是默认的表头无法添加复选框,只能用图片画上去一个复 ...

  3. 虚拟机中不能连接usb设备解决办法

    虚拟机中不能连接usb设备解决办法 1.点击开始->运行,在对话框中输入"services.msc",确定,打开windows服务管理器.2.在服务列表中选中"VM ...

  4. VIM编辑器和VI编辑器的区别

    vi 和vim 的区别 写在前面:这个两个"东西"着实让我烦恼一阵子,但是自己一直没当回事,但是遇到了好几次再决定彻底把他们搞的明白,一下是我通过查找资料了解到的关于这两个编辑器的 ...

  5. Redis入门指南之二(安装及配置)

    本节主要内容 1. 前言2. redis安装3. 启动和停止Redis 1. 前言 安装Redis需要知道自己需要哪个版本,有针对性的安装,比如如果需要redis GEO这个地理集合的特性,那么red ...

  6. JS获取当前日期方法

    Date.prototype.format = function (format) { var args = { "M+": this.getMonth() + 1, " ...

  7. LeetCode--458--可怜的小猪

    问题描述: 有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你 ...

  8. BFS+二进制状态压缩 hdu-1429

    好久没写搜索题了,就当练手吧. vis[][][1025]第三个维度用来维护不同key持有状态的访问情况. 对于只有钥匙没有对应门的位置,置为'.',避免不必要的状态分支. // // main.cp ...

  9. 【IDEA】【7】Git更新及提交

    如果是Git管理的项目,顶部会出现这样的按钮 绿色代表commit到本地 蓝色代表update最新代码 Push:推送到远程服务器:右键项目->Git->Repository->Pu ...

  10. HDU-6395-矩阵快速幂

    Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...