模板来源:http://www.neroysq.com/?p=76

思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html

题意就是求两个字符串的最长公共子串,串长最大250000。

以串A构建一个后缀自动机,用串B来匹配。枚举串B的每一位B[i]即考虑串B中所有以B[i]为结尾的子串,维护的值为以B[i]为末尾能匹配的最大长度tmpL。

假设走到B[i]时已经匹配好的串为str,如果当前节点有B[i]这个儿子,直接向下走,++tmpL。

如果没有,沿着fail指针向前回退,直到找到一个有B[i]儿子的节点。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ;
const int SigmaSize = ; struct sanode
{
sanode *f, *ch[SigmaSize];
int l;
}; struct Suffix_Automaton
{
sanode pool[ ( MAXN << ) + ];
sanode *init;
sanode *tail;
int tot; void Clear()
{
tot = ;
init = pool;
tail = init;
return;
} void Insert( int c )
{
sanode *q = pool + ( ++tot ), *p = tail;
q->l = p->l + ;
for ( ; p && !p->ch[c]; p = p->f ) p->ch[c] = q;
tail = q;
if ( !p ) q->f = init;
else
{
if ( p->ch[c]->l == p->l + ) q->f = p->ch[c];
else
{
sanode *r = pool + ( ++tot ), *u = p->ch[c];
*r = *u;
r->l = p->l + ;
u->f = q->f = r;
for ( ; p && p->ch[c] == u; p = p->f ) p->ch[c] = r;
}
}
}
}; char str[MAXN + ];
Suffix_Automaton SAM; int main()
{
int len;
scanf( "%s", str + ); SAM.Clear();
len = strlen( str + );
for ( int i = ; i <= len; ++i )
SAM.Insert( str[i] - 'a' );
sanode *p = SAM.init;
scanf( "%s", str + );
len = strlen( str + );
int tmpL = , ans = ;
for ( int i = ; i <= len; ++i )
{
if ( p->ch[ str[i] - 'a' ] ) //可以向下匹配的时候就继续向下匹配
p = p->ch[ str[i] - 'a' ], ++tmpL;
else //如果当前p没有str[i]这个孩子
{
while ( p && !p->ch[ str[i] - 'a' ] )
          p = p->f; //沿着fail指针向前找,直到找到有str[i]儿子的结点,或者到根节点
if( p ) //如果能找到一个有str[i]儿子的节点
{
tmpL = p->l + ;
p = p->ch[ str[i] - 'a' ];
}
else //直到回到根也没有找到
{
p = SAM.init;
tmpL = ;
}
}
ans = max( ans, tmpL );
} printf( "%d\n", ans );
return ;
}

SPOJ 1811 Longest Common Substring 后缀自动机的更多相关文章

  1. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  2. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

  3. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  4. SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)

    http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...

  5. ●SPOJ 1811 Longest Common Substring

    题链: http://poj.org/problem?id=2774 题解: 求两个字符串(S,T)的最长公共子串.对 S串建后缀自动机.接下来就用这个自动机去求出能和 S串匹配的 T的每一个前缀的最 ...

  6. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...

  7. SPOJ 1811 Longest Common Substring

    Description 给出两个字符串,求最长公共子串. Sol SAM. 这题随便做啊...后缀数组/Hash+二分都可以. SAM就是模板啊...直接在SAM上跑就行,没有了 \(go[w]\) ...

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

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  9. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

随机推荐

  1. 剑指offer--面试题21--相关

    题目:设计包含min函数的栈,pop(),push(),min()的时间复杂度均为O(1) 自己所写代码如下:(写‘栈’的代码还是有些不熟练!) #include <iostream> u ...

  2. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  3. Brush Mode --- Nyoj 236 分类: Brush Mode 2014-04-02 06:56 116人阅读 评论(0) 收藏

    心急的C小加 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间 ...

  4. [百度空间] [转]程序员趣味读物:谈谈Unicode编码

    出处:CSDN [ 2005-05-13 10:05:53 ] 作者:fmddlmyy 这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG ...

  5. HDU1004 Let the Balloon Rise(map的简单用法)

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  6. RCC 2014 Warmup (Div. 2)

    一场很很多HACK的比赛,PREtest太弱了,真的很多坑!平时练习的时候很少注意这些东西了! A:开始一直在模拟,后来发现自己的思路逻辑很乱,果然做比赛不给力! 直接在代码中解释了 #include ...

  7. CSS绝对定位和相对定位 position: absolute/relative

    absolute(绝对定位): 会把对象拖离HTML文档流,并通过top, left, right, bottom确定对象的具体位置,这个四个位置属性至少要设置一个,否则无法激活对象的absolute ...

  8. Sqli-labs less 41

    Less-41 此处与less-39是一致的,区别在于41错误不回显.所以我们称之为盲注. Payload: http://192.168.11.189/sqli-labs/Less-41/index ...

  9. 2016年度 JavaScript 展望(上)

    [编者按]本文作者为资深 Web 开发者 TJ VanToll, TJ 专注于移动端 Web 应用及其性能,是<jQuery UI 实践> 一书的作者. 本文系 OneAPM 工程师编译呈 ...

  10. 7 天玩转 ASP.NET MVC — 第 4 天

    目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 欢迎来到第四天的 MVC 系列学习中.如果你直接开始学习今天的课程,我强烈建议你先完成之前的学习内 ...