题解:

后缀自动机

先把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. nRF52832-PPI部分学习

    PPI部分学习思维导图 PPI原理 1.1PPI简介 PPI实现的就是通过初始化配置,将不同外设的事件和任务连接起来,让事件自动去触发任务的功能,PPI有多个通道, 每个通道包含一个EEP和TEP,使 ...

  2. BootStrap table隐藏列两种方式 (踩坑)

    1.第一种  利用 visible 属性 { field : 'userAccount', title : '订阅人', visible : visible(), formatter:function ...

  3. vue2总结

    1.ref可跨页面取.vue文件的所有内容: <!--echart图自定义公用模板--> <echar-tem ref="echar"> </echa ...

  4. 修改TP5中common模块默认不能使用问题

    在TP5框架中common模块是一个特殊的模块,默认是禁止直接访问的,一般用于放置一些公共的类库用于其他模块的继承.其实是可以访问common模块的, 只需要把convention.php文件中的 / ...

  5. 日常英语---十、MapleStory/Monsters/Level 191-201(Sad Erda:向你发射炮弹进行攻击)

    日常英语---十.MapleStory/Monsters/Level 191-201(Sad Erda:向你发射炮弹进行攻击) 一.总结 一句话总结:一种悲伤的精神,形成于奥术之河,一个黑法师能够融合 ...

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

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

  7. WGCNA | weighted correlation network analysis

    早就接触过这个包了,只是一直畏惧,以为很复杂,也没有应用场景,所以认识很肤浅. 现在有应用场景了,我自己开发了一个幼稚的版本,发现了很多需求,后来想起了WGCNA似乎部分解决了我的问题,今天认真打开了 ...

  8. hdu-4507 吉哥系列故事——恨7不成妻 数位DP 状态转移分析/极限取模

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 求[L,R]中不满足任意条件的数的平方和mod 1e9+7. 条件: 1.整数中某一位是7:2.整数的每一 ...

  9. Confluence 6 查看所有空间

    有下面 2 种方法在 Confluence 中查看空间: 空间目录(The space directory) – 在 Confluence 的头部选择 空间(Spaces )> 空间目录(Spa ...

  10. 第二阶段——个人工作总结DAY03

    1.昨天做了什么:昨天实现了一个活动跳转到另一个活动. 2.今天做了什么:今天打算将所有的都实现,并且只用一个监听事件封装. 3.遇到的困难:无.