【SP1811】LCS - Longest Common Substring
【SP1811】LCS - Longest Common Substring
题面
题解
建好后缀自动机后从初始状态沿着现在的边匹配,
如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(Endpos\)集合中的串肯定是当前\(Endpos\)中的后缀,所以这么做是对的。
你感性理解一下,这样显然是最大的是吧。。。
具体实现看代码:
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX_N = 2.5e5 + 5;
struct Node { int ch[26], fa, len; } t[MAX_N << 1];
int lst = 1, tot = 1;
void extend(int c) {
++tot, t[lst].ch[c] = tot;
t[tot].len = t[lst].len + 1;
int p = t[lst].fa; lst = tot;
while (p && !t[p].ch[c]) t[p].ch[c] = tot, p = t[p].fa;
if (!p) return (void)(t[tot].fa = 1);
int q = t[p].ch[c];
if (t[q].len == t[p].len + 1) return (void)(t[tot].fa = q);
int _q = ++tot;
t[_q] = t[q], t[q].fa = t[tot - 1].fa = _q;
t[_q].len = t[p].len + 1;
while (p && t[p].ch[c] == q) t[p].ch[c] = _q, p = t[p].fa;
}
char s1[MAX_N], s2[MAX_N];
int N;
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
scanf("%s%s", s1 + 1, s2 + 1);
N = strlen(s1 + 1);
for (int i = 1; i <= N; i++) extend(s1[i] - 'a');
N = strlen(s2 + 1);
int ans = 0, l = 0, v = 1;
for (int i = 1; i <= N; i++) {
while (v && !t[v].ch[s2[i] - 'a']) v = t[v].fa, l = t[v].len;
if (!v) v = 1, l = 0;
if (t[v].ch[s2[i] - 'a']) v = t[v].ch[s2[i] - 'a'], ++l;
ans = max(ans, l);
}
printf("%d\n", ans);
return 0;
}
【SP1811】LCS - Longest Common Substring的更多相关文章
- 【SP1811】 LCS - Longest Common Substring(SAM)
传送门 洛谷 Solution 考虑他要求的是最长公共子串对吧,那么我们对于一个串建后缀自动机,另一个串在后缀自动机上面跑就是了. 复杂度\(O(n+m)\)的,很棒! 代码实现 代码戳这里
- 【后缀数组】【SP1811】 LCS - Longest Common Substring
题目链接 题意翻译 输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串.如果没有公共子串则输出0 . 思路 求两个串的最长公共子串 代码 #include<iostrea ...
- 【SP1811】 LCS - Longest Common Substring(后缀自动机)
题目链接 对第一个串建出\(SAM\),然后用第二个串去匹配. 如果能往下走就往下走,不能的话就跳parent tree的父亲,直到能走为止.如果跳到\(0\)了还是不能走,重新匹配. #includ ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- 【HDOJ】1403 Longest Common Substring
后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 28 ...
- 后缀自动机(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 ...
随机推荐
- CSS样式----css样式表和选择器(图文详解)
本文最初于2015-10-03发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 本文主要内容 CSS概述 CSS和HTML结合的三种方式:行内样 ...
- Oracle EBS AR 收款取数
-- 收款核销,贷项通知单核销也是通过ar_receivable_applications_all表 SELECT cr.receipt_number ,ad.amount_dr ,ad.amount ...
- Linux 中 FQDN 查询及设置
FQDN:(Fully Qualified Domain Name)全限定域名:同时带有主机名和域名的名称 其实就是标注一个主机的完整域名.比如我的域名为 ifrom.top 那么它的邮件服务器的主机 ...
- [IDEA_1] IDEA 使用指南
1. IDEA 安装与配置 具体细节待补充... 2. 优化编程体验 2.1.1 新建类后自动添加自定义的注释 在主界面使用快捷键 Ctrl + Alt + S 进入 Settings 页面 依次打开 ...
- [WPF]DropShadowEffect导致Image模糊问题
实现鼠标在图片上时,图片外侧有发光效果,如上图 可使用触发器修改Image控件的Effect属性 <Style.Triggers> <Trigger Property="I ...
- python基本语法:
http://www.runoob.com/python/python-basic-syntax.html
- 团队作业——Alpha冲刺 7/12
团队作业--Alpha冲刺 冲刺任务安排 杨光海天 今日任务:将编辑界面与标题栏合并.与同队成员,讨论部分功能合并的问题. 明日任务:编辑界面与另一队员完成的字体设置弹窗合并. 郭剑南 今日任务:使用 ...
- BZOJ 1028 麻将 枚举
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1028 题目大意: 麻将是中国传统的娱乐工具之一.麻将牌的牌可以分为字牌(共有东.南.西 ...
- 关于$namespace$和重载运算符
$namespace$ 还记得列队和天天爱跑步吗?记得当时写部分分写的非常艰难,一大原因就是部分分之间有很多重名的数组,而且大小还不一样大,经常写着写着就串了,而且$maxn$有一次提交时用错了直接全 ...
- Android开发之自定义局部导航菜单
如今,要实现导航功能方案有很多.比如: 1.用3.0+自带的Toolbar + Fragment导航. 2.用Tabhost实现导航.小弟学浅,就只用过这两种方案实现导航. 但是这两种方案都有一个很明 ...