【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的更多相关文章

  1. 【SP1811】 LCS - Longest Common Substring(SAM)

    传送门 洛谷 Solution 考虑他要求的是最长公共子串对吧,那么我们对于一个串建后缀自动机,另一个串在后缀自动机上面跑就是了. 复杂度\(O(n+m)\)的,很棒! 代码实现 代码戳这里

  2. 【后缀数组】【SP1811】 LCS - Longest Common Substring

    题目链接 题意翻译 输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串.如果没有公共子串则输出0 . 思路 求两个串的最长公共子串 代码 #include<iostrea ...

  3. 【SP1811】 LCS - Longest Common Substring(后缀自动机)

    题目链接 对第一个串建出\(SAM\),然后用第二个串去匹配. 如果能往下走就往下走,不能的话就跳parent tree的父亲,直到能走为止.如果跳到\(0\)了还是不能走,重新匹配. #includ ...

  4. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  5. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

  6. 【HDOJ】1403 Longest Common Substring

    后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 28 ...

  7. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  8. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

  9. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

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

随机推荐

  1. [Android] 图片裁剪总结——自定义裁剪工具

    上次弄完调用系统裁剪之后,我又试着做一个自定义的裁剪工具. 原文地址请保留http://www.cnblogs.com/rossoneri/p/3988405.html 老习惯,文章开始前还是先把我参 ...

  2. [WPF 知识总结系列] —— 基本控件的简单样式集合

    一.ScrollBar <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta ...

  3. sklearn——数据集调用及应用

    忙了许久,总算是又想起这边还没写完呢. 那今天就写写sklearn库的一部分简单内容吧,包括数据集调用,聚类,轮廓系数等等.   自带数据集API 数据集函数 中文翻译 任务类型 数据规模 load_ ...

  4. c#WebApi使用form表单提交excel,实现批量写入数据库

    思路:用户点击下载模板按钮,获取到excel模板,然后向里面填写数据保存.from表单提交的时候选择保存好的excel,实现数据的批量导入过程 先把模板放在服务器的项目目录下面:如 模板我一般放在:F ...

  5. SQL Server 跨网段(跨机房)通过备份文件初始化复制

    笔者最近碰到了需要搭建跨网段的SQL Server复制,实际的拓扑结构如下草图所示: 发布端A服务器位于CDC机房中 订阅端B服务器位于阿里云 因为SQL Server复制不支持通过IP连接分发服务器 ...

  6. .net反编译工具

    1:.Net Reflector [收费]官方网址:http://www.red-gate.com/products/dotnet-development/reflector/ 2:ILSpy/dnS ...

  7. Linux清除用户登录记录和命令历史方法(个人笔记)

    清除登陆系统成功的记录 [root@localhost root]# echo > /var/log/wtmp //此文件默认打开时乱码,可查到ip等信息 [root@localhost roo ...

  8. VS 0x80041FEB

    在打开from设计界面时,报错. 解决方法:将项目中Properties文件中licenses.licx删除,重新建立一个空的licenses.licx文件放到项目中. 重新打开界面,解决

  9. 【2017下集美大学软工1412班_助教博客】团队作业4——Alpha冲刺日志公示

    作业要求 团队作业4--第一次项目冲刺(Alpha版本) 团队评分结果和评分标准 检查项 总分 会议内容 代码签入 心得体会或其他记录 燃尽图 会议照片 评论区反馈 组别 分值 10 2 2 2 1 ...

  10. div设置contenteditable="true" 光标消失:原因

    原因1:document.onselectstart= function(){return false;}; 原因2:父层设置了user-select:none 导致 子层设置了 contentedi ...