SP1811 LCS - Longest Common Substring
\(\color{#0066ff}{ 题目描述 }\)
输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串。如果没有公共子串则输出0 。
\(\color{#0066ff}{输入格式}\)
两个字符串
\(\color{#0066ff}{输出格式}\)
一个整数,为 所求答案
\(\color{#0066ff}{输入样例}\)
alsdfkjfjkdsal
fdjskalajfkdsla
\(\color{#0066ff}{输出样例}\)
3
\(\color{#0066ff}{数据范围与提示}\)
none
\(\color{#0066ff}{ 题解 }\)
对第一个串建立后缀自动机
用第二个串在自动机上跑匹配
匹配一下就取max
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 6e5 + 5;
struct SAM {
protected:
struct node {
node *ch[26], *fa;
int len, siz;
node(int len = 0, int siz = 0): fa(NULL), len(len), siz(siz) {
memset(ch, 0, sizeof ch);
}
};
node *root, *tail, *lst;
node pool[maxn];
void extend(int c) {
node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
if(!v) o->fa = root;
else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
else {
node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
std::copy(d->ch, d->ch + 26, n->ch);
n->fa = d->fa, d->fa = o->fa = n;
for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
}
lst = o;
}
void clr() {
tail = pool;
root = lst = new(tail++) node();
}
public:
SAM() { clr(); }
void ins(char *s) { for(char *p = s; *p; p++) extend(*p - 'a'); }
int match(char *s) {
int ans = 0;
node *o = root;
int len = 0;
for(char *p = s; *p; p++) {
int pos = *p - 'a';
if(o->ch[pos]) o = o->ch[pos], len++;
else {
while(o && !o->ch[pos]) o = o->fa;
if(!o) o = root, len = 0;
else len = o->len + 1, o = o->ch[pos];
}
ans = std::max(ans, len);
}
return ans;
}
}sam;
char s[maxn];
int main() {
scanf("%s", s);
sam.ins(s);
scanf("%s", s);
printf("%d\n", sam.match(s));
return 0;
}
SP1811 LCS - Longest Common Substring的更多相关文章
- 【题解】SP1811 LCS - Longest Common Substring
\(\color{purple}{Link}\) \(\text{Solution:}\) 题目要求找到两个串的最长公共子串.\(LCP\) 我们将两个串中间和末尾插入终止符,并弄到一棵后缀树上去. ...
- 「双串最长公共子串」SP1811 LCS - Longest Common Substring
知识点: SAM,SA,单调栈,Hash 原题面 Luogu 来自 poj 的双倍经验 简述 给定两字符串 \(S_1, S_2\),求它们的最长公共子串长度. \(|S_1|,|S_2|\le 2. ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ 1811 LCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
随机推荐
- Jenkins构建触发器定时Poll SCM、Build periodically
一.时间设置语法 时间设置由5位组成:* * * * * 第一位:表示分钟,取值0-59. 第二位:表示小时,取值0-23. 第三位:表示日期,取值1-31. 第四位:表示月份,取值1-12. 第五位 ...
- 如何平衡MMO游戏
转自:http://blog.sina.com.cn/s/blog_9f56a65f01013eti.html 前言 这是一篇出自国外博客“Elder Game”的一篇文章,笔者觉得里面很多东西说的很 ...
- iOS利用SDWebImage图片下载缓存
一.我们先来了解一下SDWebImage的使用: 1.导入框架,引入头文件: #import "UIImageView+WebCache.h" 也可以直接使用CocoaPods来引 ...
- C语言在32位和64位机器下数字数据类型的字节数取决于编译器和平台, 主要由编译器决定。
C语言中数字数据类型的字节数 C声明 32位机器 64位机器 char 1 1 short int 2 2 int 4 4 //整型在32位和64位下都是4个字节 long int 4 8 l ...
- navicat for mysql ,mysql版本是8.0的版本,连接数据库报错1251,解决办法。
我的mysql版本是8.0的版本,因为毕竟新的mysql采用新的保密方式,所以就的似乎不能用,改密码方式: 用管理员身份打开cmd mysql -uroot -p(输入密码) 进 ...
- css的relative与absolute(一)
relative与absolute是position的两个值,本文对这两个值得关系进行了一个小实验 实验一: 首先定义了两个div元素,代码如下所示: <!doctype html> &l ...
- Bind和Eval的不同用法 (转)
今天在用DataList的模板列的时候习惯性地像在03中那样去给模板列的绑定字段加个处理函数: < asp:Label ID = " Label1 " runat = &qu ...
- 使用GSON来生成JSON数据
第二种方法: 当不需要显示某个属性时,在不需要显示出的属性前加transient关键字即可满足 使用gson来解析 使用gson解析 带日期转换 集合类解析:gson中的数组与java中集合类都是对应 ...
- 由浅入深漫谈margin属性
margin 在中文中我们翻译成外边距或者外补白(本文中引用外边距).他是元素盒模型(box model)的基础属性. 一.margin的基本特性 margin 属性包括 margin-top, ma ...
- Android屏幕适配终结者
1,http://blog.csdn.net/zhengjingle/article/details/51742839 github : https://github.com/zhengjingle/ ...