题目链接

对第一个串建出\(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. 带状矩阵的存储(c++)

    2     1     0     0 3     1     3     0 0     5     2     7 0     0     9     0 这个程序对于三对角矩阵都是有效的,为了精 ...

  2. ubuntu之路——day13 只用python的numpy在较为底层的阶段实现单隐含层神经网络

    首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...

  3. 【转】ANDROIDROM制作(一)——ROM结构介绍、精简和内置、一般刷机过程

    作为对Rom制作的一个总结,本节主要介绍以下内容:  1.Rom介绍  2.Rom文件结构  3.app的精简与内置  4.Recovery简介  5.radio包简介  6.一般刷机过程.刷机过程中 ...

  4. vs2015 编译obs studio 遇到的几个错误

    1. >D:\project\vs\obs\ObsProject\obs-studio\plugins\win-wasapi\win-wasapi.cpp(245): error C2065: ...

  5. semi-join子查询优化 -- Duplicate Weedout策略

    duplicate weedout是执行semi-join子查询的一种策略. 将semi-join作为一个常规的inner join.然后使用一个临时表,将重复的记录排除. 假设,你有一个查询,你在寻 ...

  6. Spring中查看加载配置文件中 加载类的个数及详情

    断点到: org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanDefinitionCount 显示该 ...

  7. k8s pv,pvc无法删除问题

    一般删除步骤为:先删pod再删pvc最后删pv 但是遇到pv始终处于“Terminating”状态,而且delete不掉.如下图: 解决方法: 直接删除k8s中的记录: 1 kubectl patch ...

  8. FPGA程序编译后逻辑单元数为0

    问题 FPGA代码写完后编译不报错,但是显示使用的逻辑单元数(Total logic elements)为0.当然程序也不工作. 我用的是Intel Altera FPGA,verilog语言,在Qu ...

  9. 学习记录-java基础部分(一)

    学习记录-java基础部分(一) 参考:GitHub上的知名项目:javaGuide : https://github.com/Snailclimb/JavaGuide/blob/master/doc ...

  10. py库:pdfminer3k、docx。(PDFf转word)

    安装pdfminer模块: pip install pdfminer3k 安装docx模块: https://www.lfd.uci.edu/~gohlke/pythonlibs/  下载 pytho ...