\(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. vue filters过滤器的使用

    说的很详细 https://www.w3cplus.com/vue/how-to-create-filters-in-vuejs.html

  2. mysql DML语句

    1, 插入数据 insert into emp1(ename,hiredate,sal,deptono) values('kingle','2000-01-01','2000',1); 插入数据加入需 ...

  3. 安装cloudermanager时出现org.spingframework.web.bind.***** host[] is not present at AnnotationMethodHandlerAdapter.java line 738 ****错误(图文详解)(博主推荐)

    不多说,直接上干货! 首先,这个问题,写给需要帮助的朋友们,本人在此,搜索资料近半天,才得以解决.看过国内和国外,资料甚少.特此,写此博客,为了弥补此错误解决的资料少的缘故! 问题详解  解决办法   ...

  4. unet知识点

    https://www.bilibili.com/video/av8483444/?from=search&seid=17755425906400905363 https://www.jian ...

  5. Nginx使用的php-fpm的两种进程管理方式及优化

    PS:前段时间配置php-fpm的时候,无意中发现原来它还有两种进程管理方式.与Apache类似,它的进程数也是可以根据设置分为动态和静态的. php-fpm目前主要又两个分支,分别对应于php-5. ...

  6. opencv2.4.10+VS2012配置问题

    opencv2.4.10+VS2012配置 作为opencv的初学者,第一个难题想必都一样,如何配置opencv+VS的环境呢?在网上的教程,铺天盖地,但我仍然是尝试了十几次才找到属于自己的那套配置方 ...

  7. Windows server 搭建ftp服务器

    1.安装ftp 2.端口端口21和20的入出端口 3.点击IIS 服务器证书 4.FTP界面上选择“FTP身份验证”——>“基本身份验证”–>”启用” 5.FTP界面选择 “FTP授权规则 ...

  8. 百度云BCC 上的Ubuntu 16.04 LTS - 升级内核到4.14.87

    99元1年,整来玩玩. =============================================================== 重点是这个libssl1.1的deb,安装上了之 ...

  9. Java 的版本历史与特性

    Java SE 8[2014-03-14发行] Lambda表达式 Pipelines和Streams Date和Time API Default方法 Type注解 Nashhorn JavaScri ...

  10. CentOS 7 学习笔记

    Centos7 命令行   快捷键: 上方向键, 查看上一条命令 Ctrl+C 强制终止程序运行   新版 nmtui 配置网络 旧版 setup(已经没了)   网络接口   ip a = ip a ...