正题

题目链接:https://www.luogu.com.cn/problem/SP1811


题目大意

求两个串的最长公共子串。


解题思路

对与第一个串首先构建一个SAMSAMSAM,然后考虑让第二个串在上面匹配。

对于枚举的每个位置要在SAMSAMSAM上找一个节点使得它的后缀是枚举到的位置的后缀。所以对于当前节点如果有新加入的字符串的边就走过去,如果没有我们就可以在parentparentparent树上往前跳,因为跳到的节点代表的后缀是满足且仅满足这些位置的后缀是加入这个字符之前的后缀,跳了之后取那个节点的lenlenlen就好了。

时间复杂度O(n)O(n)O(n)


codecodecode

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e5+10;
int n,las,tot,len[N],fa[N];
char s[N];int ch[N][26],ans;
void add(int c){int p=las;int np=las=++tot;len[np]=len[p]+1;for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;if(!p)fa[np]=1;else{int q=ch[p][c];if(len[p]+1==len[q])fa[np]=q;else{int nq=++tot;len[nq]=len[p]+1;memcpy(ch[nq],ch[q],sizeof(ch[q]));fa[nq]=fa[q];fa[q]=fa[np]=nq;for(;p&&ch[p][c]==q;p=fa[p])ch[p][c]=nq;}}return;
}
int main()
{las=tot=1;scanf("%s",s+1);n=strlen(s+1);for(int i=1;i<=n;i++)add(s[i]-'a');memset(s,0,sizeof(s));scanf("%s",s+1);n=strlen(s+1);int now=1,L=0;for(int i=1;i<=n;i++){int c=s[i]-'a';if(ch[now][c])now=ch[now][c],++L;else{for(;now&&!ch[now][c];now=fa[now]);if(now)L=len[now]+1,now=ch[now][c];else L=0,now=1;}ans=max(ans,L);}printf("%d",ans);
}

SP1811-Longest Common Substring【SAM】相关推荐

  1. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)【两种做法】

    SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)[两种做法] 手动博客搬家: 本文发表于20181217 23:54:35, 原地址https: ...

  2. SPOJ LCS Longest Common Substring

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

  3. Spoj LCS2 - Longest Common Substring II

    题目描述 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is ...

  4. spoj Longest Common Substring II

    Longest Common Substring II SPOJ - LCS2 求10个串的LCS /*1.用第一个串建立后缀自动机2.len[s] 表示状态s 所能代表的字符串的最大长度mx[s] ...

  5. JavaScript实现longest Common Substring最长公共子串算法(附完整源码)

    JavaScript实现longest Common Substring最长公共子串算法(附完整源码) longestCommonSubstring.js完整源代码 longestCommonSubs ...

  6. UVA10405 Longest Common Subsequence【LCS+DP】

    Given two sequences of characters, print the length of the longest common subsequence of both sequen ...

  7. 【SAM】51Nod1647 小Z的Trie

    [前言] 本来是用来愉悦身心的题目,结果因为自己一些zz错误弄得很不愉悦. [题目] 51Nod 给定一棵Trie\text{Trie}Trie(实际上是给出字符串自己建),QQQ次询问Trie\te ...

  8. 【SAM】BZOJ5137 [Usaco2017 Dec] Standing Out from the Herd

    [题目] lydsy 给定 n n n个字符串,对于每个字符串,问只在这个字符串中出现的子串有多少个. n , ∑ ∣ S ∣ ≤ 1 0 5 n,\sum|S|\leq 10^5 n,∑∣S∣≤10 ...

  9. SPOJ1812 LCS2 - Longest Common Substring II(SAM)

    题目链接 分析: SAM求多串最长公共子串 详解戳这里 #include<cstdio> #include<cstring> #include<iostream>u ...

最新文章

  1. 真我新格调 勇敢使梦想×××
  2. Linux静态/动态链接库的创建和使用
  3. js正则--验证6-12位至少包含数字、小写字母和大些字母中至少两种字符,
  4. softmax logistic loss详解
  5. 2017一季度JAVA面试题锦集
  6. pom.xml文件详解
  7. weblogic控制台查看日志_weblogic 控制台日志保存到文件
  8. python导入mat文件_python读取并写入mat文件的方法
  9. 关于嵌入式学习随笔-1《STM32简介》
  10. orb特征描述符 打开相机与图片物体匹配
  11. C#反编译工具:ILSpy
  12. Linux内核网络编程
  13. windows下面刷新dns
  14. 马哥2020linux笔记下载,马哥linux 基础命令笔记
  15. 02-02 逐帧动画、多组动画案例 实现小人跑步效果
  16. R语言smoothHR包_R语言数据分析实例一:离职率分析与建模预测
  17. 快速生产地图瓦片解决方案:多任务切图
  18. C语言小游戏: 2048.c
  19. 深入了解 vue-cli
  20. 计算机屏幕怎么拆,aoc显示器怎么拆解?拿掉器的拆解方法!

热门文章

  1. mysql slave 1032_修复mysql slave复制1032错误
  2. pixelbook安装linux系统,谷歌Pixelbook可以运行Fuchsia操作系统 正测试
  3. cent os重置mysql,linux mysql 能登陆不能修改用户(cent os 6.2)解决思路
  4. .net html5页面缓存,详解HTML5中的manifest缓存使用
  5. matlab优化算法案例分析与应用_最优化计算与matlab实现(18)——粒子群优化算法——权重改进的粒子群算法...
  6. 北航计算机学院有河南的,北航计划在豫招生165人 河南多所高职公布预录名单...
  7. 算法设计与分析——递归与分治策略——循环日程赛
  8. Java手撕Linkedlist(双向链表)基本用法的实现
  9. [Redis6]常用数据类型_String字符串
  10. [汇编语言]实验:更灵活的寻址方式 -应用si和di