模板来源: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. springmvc返回jsp源代码解决办法

    url-pattern问题 spring用到forward("/WEB-INF/jsp/*.jsp")而forward当然是又要经过web.xml的映射的,然后,在URL匹配时,  ...

  2. Tomcat配置HTTPS方式生成安全证书

    在Tomcat 6中配置SSL双向认证是相当容易的,本文将介绍如何使用JDK的keytool来为Tomcat配置双向SSL认证.并实现批量生成证书 系统需求:JDK 5.0Tomcat 6.0.16启 ...

  3. mysql的简单主从复制(ubuntu)

    环境:两台ubuntu 12.04.5 虚拟机    mysql-server-5.5 master (192.168.240.130) slave (192.168.240.129) (1)查看二进 ...

  4. django构建blog--页面部分(eclipse+pydev)

    本文介绍的是在eclipse+pydev 平台下,利用django 搭建blog的第2部分:页面部分(主要涉及3个部分:模板.视图.URL模式) 篇幅1:创建模板 blog目录下新建一个文件夹:tem ...

  5. 【Android】cocos2d-x-3.1.1环境搭建与创建工程( Win7 32位系统)

    参考资料: http://blog.csdn.net/wxc237786026/article/details/32907079 1.环境搭建 2.创建工程 2.1 VS2012运行 2.2 Andr ...

  6. matrix_world_final_2012

    B http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98759#problem/B 题意:瓶子侧躺在数轴上,瓶底在xlow,瓶口在xhigh ...

  7. JavaScript在IE6,IE7下报错'expected identifier, string or number'

    问题: 代码在Forefox和IE8下工作正常,但是在IE6下报错: expected identifier, string or number 假如变量options有多个选项,那么我们可以用逗号分 ...

  8. [百度空间] [原]DLL导出实例化的模板类

    因为模板是在编译的时候根据模板参数实例化的,实例化之后就像一个普通的类(函数),这样才有对应的二进制代码;否则,没有模板参数,那么编译器就不知道怎么生成代码,所以生成的DLL就没有办法导出模板了.但是 ...

  9. unset之讲解

    unset (PHP 4, PHP 5) unset — 释放给定的变量 说明¶ void unset ( mixed $var [, mixed $... ] ) unset() 销毁指定的变量. ...

  10. mac mysql connection

    随着网络日益发展还有os x用户的增多,有可能会需要在自己的x系统中运行mysql+php环境,比如架设网站或者测试之类.简单步骤如下: 1.下载MySQL 5.x 发行版,解压并安装映像中的两个安装 ...