SPOJ LCS Longest Common Substring(后缀自动机)题解
题意:
求两个串的最大\(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(后缀自动机)题解的更多相关文章
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- SPOJ 1811 Longest Common Substring 后缀自动机
模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- SPOJ LCS - Longest Common Substring 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...
- [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
随机推荐
- 1.5V升5V芯片,1.5V升5V电路图规格书
常用的 5号,7号等 1.5V 干电池满电电压在 1.6V 左右,干电池输出耗电电压在 1V.适用PW5100,在 0.9V 时还能输出,彻底榨干干电池的电量. 1.5V 升5V 的芯片:PW5100 ...
- linux opt, usr文件夹说明
linux下各文件夹介绍: https://www.pathname.com/fhs/pub/fhs-2.3.html /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理解为 ...
- Angular入门到精通系列教程(13)- 路由守卫(Route Guards)
1. 摘要 2. 路由守卫(Route Guards) 2.1. 创建路由守卫 2.2. 控制路由是否可以激活 2.3. 控制路由是否退出(离开) 3. 总结 环境: Angular CLI: 11. ...
- SuperUpdate.sh 一键更换Linux软件源脚本
一.前言 有时候会遇到 Linux 的源更新速度非常的缓慢,特别是在国内使用默认的源,因为国内的网络环境,经常会出现无法更新,更新缓慢的情况.在这种情况下,更换一个更适合或者说更近,更快的软件源,会为 ...
- vue-cli快速创建项目,交互式
vue脚手架用于快速构建vue项目基本架构 下面开始安装vue-cli npm install -g @vue/cli # OR yarn global add @vue/cli以上两句命令都可以安装 ...
- Qt 自动化测试Test cutedriver
示例 https://github.com/nomovok-opensource/cutedriver-examples CuteDriver examples This repository con ...
- map 传递给函数的代价
https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/08.1.md map 传递给函数的代价很小:在 32 位机器上占 4 ...
- Supporting Multiple Versions of WebSocket Protocol 支持多版本WebSocket协议
https://tools.ietf.org/html/rfc6455#section-4.4 4.4. Supporting Multiple Versions of WebSocket Proto ...
- What is the difference between btree and rtree indexing?
https://softwareengineering.stackexchange.com/questions/113256/what-is-the-difference-between-btree- ...
- trust an HTTPS connection 安全协议 随机数 运输层安全协议 应用层安全协议 安全证书
小结: 1.HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间) HTTPS(全称:Hyper Text Transfer Protocol over Secure ...