\(SAM\)上匹配

我们就是需要找到两个串的最长公共子串

先对其中一个串建出\(SAM\),之后我们把另一个串放到上面跑

如果当前在\(SAM\)的状态是\(now\),下一个字符是\(c\),匹配出的的长度为\(L\)

  • 如果\(now\)有\(c\)这个转移,我们就转移过去,\(L\)++

  • 如果没有我们就跳\(link\),知道跳到有这个转移为止,同时把\(L\)搞成新状态的\(len\)

这样做就好了

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define LL long long
#define maxn 500005
#define max(a,b) ((a)>(b)?(a):(b))
char S[maxn>>1],T[maxn>>1];
int lst=1,n,m,len[maxn],pre[maxn],son[maxn][26];
int now=1,L,ans,cnt=1;
inline void ins(int c)
{
int f=lst,p=++cnt; lst=p;
len[p]=len[f]+1;
while(f&&!son[f][c]) {son[f][c]=p;f=pre[f];}
if(!f) {pre[p]=1;return;}
int x=son[f][c];
if(len[f]+1==len[x]) {pre[p]=x;return;}
int y=++cnt;
len[y]=len[f]+1;pre[y]=pre[x];pre[x]=pre[p]=y;
for(int i=0;i<26;i++) son[y][i]=son[x][i];
while(f&&son[f][c]==x) {son[f][c]=y;f=pre[f];}
}
inline void q(int c)
{
if(son[now][c]) {now=son[now][c];L++;ans=max(ans,L);return;}
while(now&&!son[now][c]) now=pre[now];
if(!now) {now=1,L=0;return;}
L=len[now]+1;now=son[now][c];ans=max(ans,L);
}
int main()
{
scanf("%s",S+1);n=strlen(S+1);
for(int i=1;i<=n;i++) ins((int)(S[i]-'a'));
scanf("%s",T+1);n=strlen(T+1);
for(int i=1;i<=n;i++) q((int)(T[i]-'a'));
printf("%d\n",ans);
return 0;
}

SP1811 【LCS - Longest Common Substring】的更多相关文章

  1. 【题解】SP10570 【LONGCS - Longest Common Substring】

    \(\color{Red}{Link}\) \(\text{Solution:}\) 还是\(\text{Suffix Tree.}\) 根据\(\color{Blue}{Link}\)我们可以得到一 ...

  2. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  3. 题解 SP1812 【LCS2 - Longest Common Substring II 】

    对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...

  4. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  5. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

  6. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  7. SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机

    Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...

  8. 【刷题】SPOJ 1811 LCS - Longest Common Substring

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

  9. SPOJ 1811 LCS - Longest Common Substring

    思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...

随机推荐

  1. linux运维配置讲解--sshd-config

    文件配置: 1, /etc/ssh/sshd_config ssh配置文件 2, /etc/shadow 密码文件 3, /etc/sudoers 授权用户管理文件 4, /etc/issue 系统信 ...

  2. LeetCode 200.岛屿的个数

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

  3. redis安装配置远程连接

    一.安装redis linux上直接yum安装 yum install redis windows版本下载地址 https://github.com/ServiceStack/redis-window ...

  4. 昨天太晚了,今天教你用Debug模式来分析程序执行顺序

    还是以昨天的XML文件解析来做栗子,希望通过这个好吃的栗子可以举一反三 学会用debug来看源码和找Bug 事件类型主要有五种START_DOCUMENT:xml头的事件类型    = 0END_DO ...

  5. WPF中嵌入Skyline提供的COM组件填坑

    网上有很多关于在WPF中使用的Skyline提供的COM组件的教程,但大都雷同一律,其中很多的细节均为涉及,本文意在在其原基础上进行一些细节的补充. 工具:Visual Studio 2017 1.  ...

  6. 【Postman】Postman的安装和使用

    Postman一款非常流行的API调试工具.其实,开发人员用的更多.因为测试人员做接口测试会有更多选择,例如Jmeter.soapUI等.不过,对于开发过程中去调试接口,Postman确实足够的简单方 ...

  7. 【VMware】安装不同系统的虚拟机出现开机黑屏的情况

    解决方法一: 1.以管理员身份运行命令提示符(cmd.exe),输入命令 netsh winsock show catalog 按下回车键执行命令(可以看到VMware注册了两个LSP:vSocket ...

  8. jQuery前端数据通用验证库,解放你的双手

    这个简易的验证库,应该能完成90%的基本验证,包括失去焦点时的验证,以及点击提交按钮时的验证.后端的那我就无能为办了,只能是谁用就谁自个儿去写了:). 先上一段调用的代码吧,JS代码说少也不少了,就不 ...

  9. 求大神帮忙解决一下c#winfrom bindingNavigatorPositionItem加载直接跳转问题

    c# winfrom bindingNavigatorPositionItem控件问题加载直接跳转到相对应的行的问题

  10. Xcode10 闪退问题

    最新更新了iOS12,mac10.13.6,xcode10之后,打开之前的项目,只要进行import,xcode就会闪退.那么就来看一下解决方案: Xcode10 新增了一个构建系统起名“New Bu ...