题目链接:点击查看

题目大意:给出两个字符串,求出其最长公共子串的长度

题目分析:可以对第一个字符串建立SAM,然后令第二个字符串在建好的SAM上跑就好了,如果遇到不能跑的点,就往上折返直到找到可以继续向下走的结点然后继续走就好了,时间复杂度为O(n)

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=3e5+100;int tot=1,last=1;struct Node
{int ch[26];int fa,len;
}st[N<<1];void add(int x)
{int p=last,np=last=++tot;st[np].len=st[p].len+1;while(p&&!st[p].ch[x])st[p].ch[x]=np,p=st[p].fa;if(!p)st[np].fa=1;else{int q=st[p].ch[x];if(st[p].len+1==st[q].len)st[np].fa=q;else{int nq=++tot;st[nq]=st[q]; st[nq].len=st[p].len+1;st[q].fa=st[np].fa=nq;while(p&&st[p].ch[x]==q)st[p].ch[x]=nq,p=st[p].fa;//向上把所有q都替换成nq}}
}char s[N],t[N];int main()
{
//  freopen("input.txt","r",stdin);
//  ios::sync_with_stdio(false);scanf("%s%s",s,t);int lens=strlen(s);int lent=strlen(t);for(int i=0;i<lens;i++)add(s[i]-'a');int ans=0,k=1,cnt=0;for(int i=0;i<lent;i++){int to=t[i]-'a';if(st[k].ch[to]){cnt++;k=st[k].ch[to];ans=max(ans,cnt);}else{while(k&&!st[k].ch[to])k=st[k].fa;if(k){cnt=st[k].len+1;k=st[k].ch[to];ans=max(ans,cnt);}else{cnt=0;k=1;}}}printf("%d\n",ans);return 0;
}

SPOJ - LCS Longest Common Substring(后缀自动机)相关推荐

  1. SPOJ LCS Longest Common Substring

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

  2. Spoj LCS2 - Longest Common Substring II

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

  3. SPOJ - LCS2 Longest Common Substring II(后缀自动机)

    题目链接:点击查看 题目大意:给出n个字符串,求出最长公共子串的长度 题目分析:之前做过了求两个字符串最长公共子串的长度,相对来说那个题目还算是比较简单入门的,这个题目就稍微有点加大难度了,其实难度也 ...

  4. POJ 2774 Long Long Message SP1811 LCS - Longest Common Substring 题解

    POJ:题目传送门 洛谷:题目传送门 题目大意: 求两个字符串的最长公共子串长度. 题解 后缀数组入门题,将两个字符串接在一起,中间用一个字符集以外的字符隔开,然后求出 h e i g h t hei ...

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

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

  6. spoj Longest Common Substring II

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

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

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

  8. 最长公共子串LCS (Longest Common Subsequence) 算法

    三个方法都有所借鉴,但代码部分是自己试着写出来的,虽然最后的运行结果都是正确的,但此过程中难免会有考虑不周全的地方,如发现代码某些地方有误,欢迎指正.同时有新的想法,也可以提出! 采用顺序结构存储串, ...

  9. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

最新文章

  1. 你真的了解javascript吗?(一)
  2. 深入浅出:Linux设备驱动之字符设备驱动
  3. Android Studio 受不了了
  4. 毕业生必须知道:干部身份、三方协议、派遣证
  5. java for与foreach_java中for和foreach的区别是什么?
  6. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 24丨不同国家的天气类型【难度简单】​
  7. sql运算符_SQL LIKE运算符概述
  8. html代码word,Web前端
  9. 计算机实战项目、毕业设计、课程设计之[含论文+辩论PPT+源码等]微信小程序社区疫情防控+后台管理|前后分离VUE[包运行成功
  10. 【my eclipse tips】Could not create the view: An unexpected exception was thrown问题
  11. 宇视摄像机实况画面不清晰排查方法
  12. 模糊控制在matlab中常见的疑难问题及模糊逻辑工具箱的使用
  13. 《30天自制操作系统》第一天
  14. Dopamine-PEG-N3,DOPA-PEG-azide,水溶性PEG试剂供应
  15. KingBaseES 报错Connection to X.X.X.X:54321 refused.Check that the hostname and port are correct and ..
  16. 浅谈面向对象与女娲造人
  17. 一个 SAP 开发工程师的成长史 - 关于信息素养和数字化资产的思考
  18. 17.商品业务-首页
  19. 当写烂代码的人离职之后....
  20. 1.4链界观区块链资讯

热门文章

  1. SpringSecurity remember功能持久化token信息
  2. 基于SpringJDBC 实现关键功能-QueryRuleSqlBulider
  3. 高仿真的类-单例工厂的顶层设计
  4. gateway网关配置入门
  5. 方法入门_方法的定义
  6. ServletContext_功能_域对象
  7. 自定义工具类:工具类介绍
  8. windows mysql读写分离_windows下的MySql实现读写分离
  9. DotNetTextBoxV3.0在线编辑器控件Ver3.4.1 Open Source开源版(附商业试用版下载)
  10. Leetcode PHP题解--D5 804. Unique Morse Code Words