SPOJ 1811 Longest Common Substring 后缀自动机
模板来源: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 后缀自动机的更多相关文章
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)
http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...
- ●SPOJ 1811 Longest Common Substring
题链: http://poj.org/problem?id=2774 题解: 求两个字符串(S,T)的最长公共子串.对 S串建后缀自动机.接下来就用这个自动机去求出能和 S串匹配的 T的每一个前缀的最 ...
- [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
- SPOJ 1811 Longest Common Substring
Description 给出两个字符串,求最长公共子串. Sol SAM. 这题随便做啊...后缀数组/Hash+二分都可以. SAM就是模板啊...直接在SAM上跑就行,没有了 \(go[w]\) ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
随机推荐
- js判断浏览器滚动条是否拉到底
$(window).scroll(function(){ // 当滚动到最底部以上n像素时, 加载新内容 if ($(document).height() - $(this).scrollTop() ...
- Ruby 多线程探索实践与归纳总结
Ruby 多线程 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程. 线程是程序中一个单一的顺序控制流程,在单个程序中同时运行多个线程完成不同的工作,称为多线程. Ruby 中我们可以通 ...
- Netsharp快速入门(之19) 平台常用功能(插件操作)
作者:秋时 暗影 转载须说明出处 6.2 插件操作 6.2.1 停用/启用 1.在平台工具-插件管理,右击对应的插件可以使用启用和停用功能.插件停用后会把所有相关的页签.程序集.服务全部停 ...
- [转载]char * 和char []的区别---之第二篇
原文地址:http://blog.sina.com.cn/s/blog_74a4593801019keb.html main() { char *p="abc123ABC";//c ...
- BZOJ3130 [Sdoi2013]费用流
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3130 这题codevs上也有,不过数据挂了[要A得看discuss]. 题目大意: Ali ...
- android解析XML总结(SAX、Pull、Dom三种方式) <转载>
android解析XML总结(SAX.Pull.Dom三种方式) http://www.cnblogs.com/JerryWang1991/archive/2012/02/24/2365507.htm ...
- 图片放大镜插件 Cloud Zoom v3.1
Cloud Zoom是一个图像放大jQuery插件,效果堪比Magic Zoom.相对于流行jQZoom插件,Cloud Zoom体积小,有更多的功能和更强大的跨浏览器兼容性. 猛击这里查看演示DEM ...
- Sencha Touch 2.4 callParent() 用法
callParent() 用法 方法介绍 用来调用父类的同名方法,并传参,这在从一个框架类派生且要重写诸如onRender这样的方法时会经常看到. 传参方式 1.arguments Ext.defin ...
- 解决iptables和vsftpd设置的问题
解决iptables和vsftpd设置的问题 博客分类: linux/centos/ubuntu 防火墙J#工作 解决iptables和vsftpd设置的问题 修改 vi /etc/sysconfig ...
- HDU 1393 Weird Clock (英语,纪念题)
这题简单,只要看懂题目就好,坑爹的是我的英语水平太差了,wa了n次,所以 仅此纪念 一下. //坑爹的英语题目,注意重点:move d times clockwise the current time ...