http://codevs.cn/problem/3160/

看了好久的后缀自动机_(:з」∠)_

对A串建立SAM,用B串去匹配A串SAM,如果在当前节点走不下去,就跳到当前节点的parent(类似AC自动机的失配指针),找到当前节点代表的状态中长度最长的后缀,并看能不能继续走下去。

如果跳到了能继续走下去的节点,就更新L为这个节点的max+1,同时在这个节点往下走一步(更新L和继续走不能颠倒,因为往下走一步之后的节点的max值并不能确定)。

照搬clj课件里的模板

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; struct State {
State *par, *go[26];
int val;
State(int _val) : par(0), val(_val)
{memset(go, 0, sizeof(go));}
} *root, *last; void extend(int w) {
State *p = last;
State *np = new State(p->val + 1);
while (p && p->go[w] == 0)
p->go[w] = np, p = p->par;
if (p == 0) np->par = root;
else {
State *q = p->go[w];
if (p->val + 1 == q->val) np->par = q;
else {
State *nq = new State(p->val + 1);
memcpy(nq->go, q->go, sizeof(q->go));
nq->par = q->par;
q->par = np->par = nq;
while (p && p->go[w] == q)
p->go[w] = nq, p = p->par;
}
}
last = np;
} int la, lb;
char A[100003], B[100003]; void work() {
State *tmp = root;
int ans = 0, x, L = 0;
for(int i = 1; i <= lb; ++i) {
x = B[i] - 'a';
if (tmp->go[x]) {
tmp = tmp->go[x];
++L;
} else {
while (tmp && tmp->go[x] == 0)
tmp = tmp->par;
if (tmp == 0) tmp = root, L = 0;
else L = tmp->val + 1, tmp = tmp->go[x];
}
ans = max(ans, L);
}
printf("%d\n", ans);
} int main() {
root = last = new State(0);
scanf("%s%s", A + 1, B + 1);
la = strlen(A + 1); lb = strlen(B + 1);
for(int i = 1; i <= la; ++i)
extend(A[i] - 'a');
work();
return 0;
}

【CodeVS 3160】最长公共子串的更多相关文章

  1. codevs 3160 最长公共子串

    3160 最长公共子串 http://codevs.cn/problem/3160/  时间限制: 2 s  空间限制: 128000 KB   题目描述 Description 给出两个由小写字母组 ...

  2. codevs 3160 最长公共子串(SAM)

    3160 最长公共子串   题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Ou ...

  3. Codevs 3160 最长公共子串(后缀数组)

    3160 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...

  4. codevs 3160 最长公共子串 后缀自动机

    http://codevs.cn/problem/3160/ 后缀自动机板子题,匹配的时候要注意如果到一个点失配向前匹配到一个点时,此时的tmp(当前匹配值)为t[j].len+1而不是t[t[j]. ...

  5. CODE【VS】3160 最长公共子串 (后缀自动机)

    3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...

  6. CODE【VS】 3160 最长公共子串 (后缀数组)

    3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...

  7. 【wikioi】3160 最长公共子串(后缀自动机)

    http://codevs.cn/problem/3160/ sam的裸题...(之前写了spoj上另一题sam的题目,但是spoj被卡评测现在还没评测完QAQ打算写那题题解时再来详细介绍sam的.. ...

  8. Codevs 1425 最长公共子串

    1425 最长公共子串  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Description 输入N(2<=N<= ...

  9. codevs 2185 最长公共上升子序列

    题目链接: codevs 2185 最长公共上升子序列codevs 1408 最长公共子序列 题目描述 Description熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升 ...

  10. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

随机推荐

  1. 《JavaScript权威指南 第六版 中文版》(一)

    <JavaScript权威指南 第六版 中文版> 第二章 词法结构 2.1字符集 JavaScript是使用Unicode字符集编码写的. 2.1.1区分大小写 JavaScript是区分 ...

  2. 微软极品工具箱-Sysinternals Suite

    工具包由来 Sysinternals Suite是微软发布的一套非常强大的免费工具程序集,一共包括74个windows工具.Sysinternals是Winternals公司提供的免费工具,Winte ...

  3. 调用newtonsoft.json反序列出错

    调用newtonsoft.json反序列出错: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current J ...

  4. Js实现简单的省市级联的效果

    需要注意的是当需要动态添加项的时候一定要先var newoption=new Option("项","值");然后再 select.options.add(ne ...

  5. C# 文件下载四方法

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  6. iOS NSFileManager 使用详解

    使用NSFileManager 文件系统接口 允许访问文件夹内容 创建 重命名 删除文件 修改文件和文件属性,以及Finder对所有文件系统任务执行的一般操作. 访问NSFileManager,使用共 ...

  7. 一个screen的简单配置。。

    # Start message startup_message off defencoding utf- encoding utf- utf- shell bash hardstatus always ...

  8. 64位MicrosoftOfficeWord加载EndnoteX7

    来源:http://jingyan.baidu.com/article/fcb5aff7a08036edaa4a71d0.html Win10 64bit 安装 Office2016 64bit 加载 ...

  9. ISAPI_Rewrite中文手册

    参考:http://blog.csdn.net/fanxiaojie119/article/details/5353186 第一章:软件介绍ISAPI_Rewrite 是一款适用于IIS的功能强大的基 ...

  10. 精通jQuery选择器

    虽然jQuery上手简单,相比于其他库学习起来较为简单,但是要全面掌握,却不轻松.因为它涉及到网页开发的方方面面,提供的方法和内部变化有上千种之多.初学者常常感到,入门很方便,提高很困难.本文的目标是 ...