题解:

后缀自动机

先把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. IOException parsing XML document from class path resource [WebRoot/WEB-INF/applicationContext.xml];

    parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io. ...

  2. 大数据-Hive 常用命令

    Hive 启动 ~$ hive 退出 hive>quit; --退出hive or hive> exit; --exit会影响之前的使用,所以需要下一句kill掉hadoop的进程 > ...

  3. redflag的echarts结构

    总体的市场情况 这里我需要4个data数组. var list = { currentData:[],//这里表示当月数据量,数组长度8 totalData:[],//这里表示的累计数据量,数组长度8 ...

  4. Linux下计划任务以及crontab权限问题

    在Linux工作环境下,我们有时可能会需要在未来某个时间执行某个命令或脚本,但是我们又不可能定个闹钟,然后到点了再去执行吧,这多麻烦.还好我们的Linux系统这么强大,提供了任务计划这个功能,我们就不 ...

  5. js获取url传递得参数

    方法一:正则法 function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(& ...

  6. 如何通过ssh远程登录内网的Mac和Linux系统?

    神器:ngrok 用法很简单,安装后 验证授权: ./ngrok authtoken 3WWujp1TEtHRo6rphEi5Y_7MBqGWm9yThCSFyqEHgip 在内网开启tcp服务 ng ...

  7. python Deep learning 学习笔记

    https://www.cnblogs.com/zhhfan/p/10300012.html

  8. ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe

    ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe 参考文章: https://patrickmn.com/as ...

  9. Could not find method google() for arguments [] on repository container.

    出这个问题主要是你Gradle版本太低的原因,一般要使用4.0+的版本 所以你需要更新你的Gradle版本至4.0+呦 tips:注意你的AndroidStudio版本应该是3.0以上,因为Gradl ...

  10. CF-822C Hacker, pack your bags! 思维题

    题目大意是给若干线段及其费用,每个线段权值即为其长度.要求找出两个不重合线段,令其权值和等于x且费用最少. 解法: 先分析一下题目,要处理不重合的问题,有重合的线段不能组合,其次这是一个选二问题,当枚 ...