题意:

求两个串的最大\(LCS\)。

思路:

把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可。

代码:

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn = 500000 + 10;
typedef long long ll;
const ll mod = 998244353;
typedef unsigned long long ull; struct SAM{
struct Node{
int next[27]; //下一节点
int fa, maxlen;//后缀链接,当前节点最长子串
void init(){
memset(next, 0, sizeof(next));
fa = maxlen = 0;
}
}node[maxn << 1];
int sz, last; void init(){
sz = last = 1;
node[sz].init();
}
void insert(int k){
int p = last, np = last = ++sz;
node[np].init();
node[np].maxlen = node[p].maxlen + 1;
for(; p && !node[p].next[k]; p = node[p].fa)
node[p].next[k] = np;
if(p == 0) {
node[np].fa = 1;
}
else{
int t = node[p].next[k];
if(node[t].maxlen == node[p].maxlen + 1){
node[np].fa = t;
}
else{
int nt = ++sz;
node[nt] = node[t];
node[nt].maxlen = node[p].maxlen + 1;
node[np].fa = node[t].fa = nt;
for(; p && node[p].next[k] == t; p = node[p].fa)
node[p].next[k] = nt;
}
}
} void solve(char *s){
int ans = 0, ret = 0;
int len = strlen(s);
int pos = 1;
for(int i = 0; i < len; i++){
int c = s[i] - 'a';
while(pos && node[pos].next[c] == 0){
pos = node[pos].fa;
ret = node[pos].maxlen;
}
if(pos == 0){
pos = 1;
ret = 0;
}
else{
pos = node[pos].next[c];
ret++;
}
ans = max(ans, ret);
}
printf("%d\n", ans);
}
}sam;
char s[maxn];
int main(){
sam.init();
scanf("%s", s);
int len = strlen(s);
for(int i = 0; i < len; i++) sam.insert(s[i] - 'a');
scanf("%s", s);
sam.solve(s);
return 0;
}

SPOJ 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 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

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

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

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

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

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

  6. SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机

    Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...

  7. SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

  8. SPOJ LCS - Longest Common Substring 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...

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

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

随机推荐

  1. vue href url地址写法

  2. 1V升压5V和1.5V升压5V的集成电路芯片

    1.5V和1V输入,要升压输出5V的集成电路芯片合适? 干电池标准电压是1.5V,放电电压后面在0.9V-1V左右,如果要选用干电池1.5V升压到5V的合适的芯片,需要满足低压1V或者0.9V更好的低 ...

  3. 阿里云OSS对象存储服务(二)

    一.使用SDK 在OSS的概览页右下角找到"Bucket管理",点击"OSS学习路径" 点击"Java SDK"进入SDK开发文档 二.创建 ...

  4. Py-多态,封装,反射,描述符,包装标准类型,面向对象进阶

    多态: 对象可以通过他们共同的属性和动作来访问,而不需要考虑他们的类多态是继承的应用 class H2o: def __init__(self,temp): self.temp=temp def ht ...

  5. 一键配置 github 可用的 hosts

    最近发现访问 Github 各种不畅通, 静态资源经常加载不出来. 写了一个一键脚本修改本机 /etc/hosts 文件, 切换到可用的 IP (数据来自 https://gitee.com/xuew ...

  6. Maven 依赖机制

    概述 在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级.让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要做什么? 传统方式 访问 ...

  7. TCP报文段的首部格式 20字节的固定首部

    <----  IP首部 TCP首部 TCP报文段的数据部分  <---- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  8.  Go is more about software engineering than programming language research.

    https://talks.golang.org/2012/splash.article Go at Google: Language Design in the Service of Softwar ...

  9. httpd反向代理实践(一)

    div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...

  10. LIS的优化

    二分优化 在求一个最长不上升自序列中,显然其结尾元素越小,越有利于接其他元素,对答案的贡献也就可能会更高 那么我们可以用low[i]去存长度为i的LIS结尾元素的最小值 因此我们只要维护low数组 对 ...