题目链接

对第一个串建出\(SAM\),然后用第二个串去匹配。

如果能往下走就往下走,不能的话就跳parent tree的父亲,直到能走为止。如果跳到\(0\)了还是不能走,重新匹配。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1000010;
struct SAM{
int ch[26];
int len, fa;
}sam[MAXN << 1];
int las = 1, cnt = 1;
inline void add(int c){
int p = las; int np = las = ++cnt;
sam[np].len = sam[p].len + 1;
for(; p && !sam[p].ch[c]; p = sam[p].fa) sam[p].ch[c] = np;
if(!p) sam[np].fa = 1;
else{
int q = sam[p].ch[c];
if(sam[q].len == sam[p].len + 1) sam[np].fa = q;
else{
int nq = ++cnt; sam[nq] = sam[q];
sam[nq].len = sam[p].len + 1;
sam[q].fa = sam[np].fa = nq;
for(; p && sam[p].ch[c] == q; p = sam[p].fa) sam[p].ch[c] = nq;
}
}
}
char a[MAXN], b[MAXN];
int ans;
int main(){
scanf("%s", a + 1);
scanf("%s", b + 1);
int lena = strlen(a + 1), lenb = strlen(b + 1);
for(int i = 1; i <= lena; ++i)
add(a[i] - 'a');
int p = 1, len = 0;
for(int i = 1; i <= lenb; ++i){
if(sam[p].ch[b[i] - 'a'])
++len, p = sam[p].ch[b[i] - 'a'];
else{
while(!sam[p].ch[b[i] - 'a'] && p) p = sam[p].fa;
if(!p) p = 1, len = 0;
else len = sam[p].len + 1, p = sam[p].ch[b[i] - 'a'];
}
ans = max(ans, len);
}
printf("%d\n", ans);
return 0;
}

【SP1811】 LCS - Longest Common Substring(后缀自动机)的更多相关文章

  1. SPOJ1811 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 1811 LCS - Longest Common Substring (后缀自己主动机)

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

  3. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  4. SPOJ 1811 Longest Common Substring 后缀自动机

    模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...

  5. SP1811 LCS - Longest Common Substring

    \(\color{#0066ff}{ 题目描述 }\) 输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串.如果没有公共子串则输出0 . \(\color{#0066ff}{输 ...

  6. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...

  7. 【题解】SP1811 LCS - Longest Common Substring

    \(\color{purple}{Link}\) \(\text{Solution:}\) 题目要求找到两个串的最长公共子串.\(LCP\) 我们将两个串中间和末尾插入终止符,并弄到一棵后缀树上去. ...

  8. 「双串最长公共子串」SP1811 LCS - Longest Common Substring

    知识点: SAM,SA,单调栈,Hash 原题面 Luogu 来自 poj 的双倍经验 简述 给定两字符串 \(S_1, S_2\),求它们的最长公共子串长度. \(|S_1|,|S_2|\le 2. ...

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

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

  10. 【SP1811】LCS - Longest Common Substring

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

随机推荐

  1. D3.js的v5版本入门教程(第七章)—— 比例尺的使用

    D3.js的v5版本入门教程(第七章) 比例尺在D3.js中是一个很重要的东西,我们可以这样理解d3.js中的比例尺——一种映射关系,从domain映射到range域(为什么会是domain和rang ...

  2. Net core学习系列(四)——Net Core项目执行流程

    "跨平台"后的ASP.Net Core是如何接收并处理请求的呢? 它的运行和处理机制和之前有什么不同?本章从"宏观"到"微观"地看一下它的结 ...

  3. 导入eclipse有Unbound classpath variable: 'M2_REPO报错的解决方法

    Eclipse maven of the project reported in Unbound classpath variable: 'M2_REPO /**/***/***. jar' But ...

  4. php怎么用正则取出网址中某个参数?

    $str = <<<TEXT 如下类似网址: https://v.qq.com/iframe/player.html?vid=j00169ib5er&tiny=0&a ...

  5. [web 前端] 封装简单的axios库

    转载自https://blog.csdn.net/qq_35844177/article/details/78809499 1.新建http.js文件,封装axios get post 方法 impo ...

  6. Spark2.x(五十六):Queue's AM resource limit exceeded.

    背景: 按照业务需求将数据拆分为60份,启动60个application分别运行对每一份数据,application的提交脚本如下: #/bin/sh #LANG=zh_CN.utf8 #export ...

  7. 详解CI、CD相关概念

    CI:持续集成(CONTINUOUS INTEGRATION)基本概念CI的全称是Continuous Integration,表示持续集成. 在CI环境中,开发人员将会频繁地向主干提交代码.这些新提 ...

  8. 用于KV集群的一致性哈希Consistent Hashing机制

    KV集群的请求分发 假定N为后台服务节点数,当前台携带关键字key发起请求时,我们通常将key进行hash后采用模运算 hash(key)%N 来将请求分发到不同的节点上, 后台节点的增删会引起几乎所 ...

  9. SpringBoot使用jasypt加解密密码

    在我们的服务中不可避免的需要使用到一些秘钥(数据库.redis等) 开发和测试环境还好,但生产如果采用明文配置讲会有安全问题,jasypt是一个通用的加解密库,我们可以使用它. <depende ...

  10. Mysql的utf8与utf8mb4区别,utf8mb4_bin、utf8mb4_general_ci、utf8mb4_unicode_ci区别

    UTF-8是使用1~4个字节,一种变长的编码格式,字符编码.mb4即 most bytes 4,使用4个字节来表示完整的UTF-8. mysql的 utf8 编码最大字符长度为 3 字节,如果遇到 4 ...