题目大意:

给出两个长度小于等于25W的字符串,求它们的最长公共子串。

题目链接:http://www.spoj.com/problems/LCS/

算法讨论:

二分+哈希, 后缀数组, 后缀自动机。

随意做。这里面只写一下我对后缀自动机做法的理解。

首先,我们假设两个串分别为A串和B串,我们先对建立出A串的后缀自动机,然后对于B串的每一位,我们进行如下的操作:首先从第1位开始,Parent树上的位置在root,那么对于每一次操作,如果当前结点的字符可以匹配当前B串中所考虑到的字符,那么自然就len ++,然后继续沿着Parent树向下走。如果当前失配,那么根据“在Parent树上某一个结点状态的pre指针指向的是可以接收同样后缀的状态结点”这条性质,我们在parent树上向上跳。此时,根据parent树原理,其父亲的right集合元素数目变多,字符串长度变短,那么,就可以这么考虑,刚刚在s(临时设个变量表示长度)长度的时候不能完全匹配,所以我们只有减小长度才有再次匹配成功的可能,所以要凭借Parent树来完成(因为Father的Right集合元素数多,所以字符串长度就短),我们沿着Parent树向上跳,这时会出现两种情况,第一,跳到了-1.也就是null状态,这说明B[i](假设当前正在考虑第i个子串)没有在A串中出现,所以此时把len清0,然后Parent树上位置回root,从新考虑。第二,找到了匹配位置,那么len = st[p].len + 1, p为当前在Parent树上的位置,为什么这样呢?考虑b[i]可以在p这个状态结点成功匹配,由于我们是从Parent上找到这个结点的,所以其前面的字符一定都是匹配的。举个例子,假设我们当前已经成功匹配了2个字符(不是定是B串中的前两个字符,因为是子串),现在考虑b[i],即可能成为成功匹配的第三个字符,如果在p成功匹配,那么由于在Parent树上向上跳了,因为上面提到的性质(Parent树的父亲的状态表示的子串是其儿子的最长后缀),子串长度会变短。假设变短1,所以匹配完后长度变成了2.。。。

Codes:

 #include <bits/stdc++.h>
using namespace std;
const int L = + ; struct State{
int len, fa;
int next[];
}st[L<<]; struct SuffixAutomaton{
int sz, last; void Init(){
last = ;
st[].len = ; st[].fa = -;
sz ++;
}
void Extend(int c){
int cur = sz ++;
st[cur].len = st[last].len + ;
int p; for(p = last; p != - && !st[p].next[c]; p = st[p].fa)
st[p].next[c] = cur; if(p == -) st[cur].fa = ;
else{
int q = st[p].next[c];
if(st[q].len == st[p].len + ) st[cur].fa = q;
else{
int cle = sz ++;
st[cle].len = st[p].len + ;
st[cle].fa = st[q].fa;
for(int i = ; i < ; ++ i) st[cle].next[i] = st[q].next[i];
for(; p != - && st[p].next[c] == q; p = st[p].fa)
st[p].next[c] = cle;
st[q].fa = st[cur].fa = cle;
}
}
last = cur;
}
}SAM; char str1[L], str2[L]; int main(){
scanf("%s%s", str1, str2); int len1 = strlen(str1), len2 = strlen(str2);
int ans = ; SAM.Init();
for(int i = ; i < len1; ++ i)
SAM.Extend(str1[i] - 'a'); int p = , len = ;
for(int i = ; i < len2; ++ i){
int x = str2[i] - 'a'; if(st[p].next[x]){
len ++;
p = st[p].next[x];
}
else{
while(p != - && !st[p].next[x]) p = st[p].fa;
if(p == -){
len = ; p = ;
}
else{
len = st[p].len + ; p = st[p].next[x];
}
}
ans = max(ans, len);
} printf("%d\n", ans);
return ;
}

SPOJ 1811

SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)的更多相关文章

  1. SPOJ 1811 Longest Common Substring 后缀自动机

    模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...

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

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

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

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

  4. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

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

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

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

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

  7. ●SPOJ 1811 Longest Common Substring

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

  8. SPOJ 1811 Longest Common Substring

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

  9. POJ 2774 求两个串的最长公共前缀 | 后缀数组

    #include<cstdio> #include<algorithm> #include<cstring> #define N 200005 using name ...

随机推荐

  1. C++中的数组和指针

    #include <iostream> #include <set> using namespace std; int main() { ] = {,,,,,}; ]; p = ...

  2. mysql to sql sersver

    USE SCK_PARA GO /****** Object:  StoredProcedure [dbo].[syncdata_mysql2sqlserver]    Script Date: 08 ...

  3. apt软件包管理

    apt软件包管理 ----   http://wiki.ubuntu.org.cn/UbuntuHelp:AptGet/Howto/zh APT HOWTO  ----    http://www.d ...

  4. unity3D对象的显示和隐藏

    SetActive/active/SetActiveRecursively 后两者比较旧,现在通常用第一个SetActive 必须先new一个gameobject对象用于实例化,然后再设置其activ ...

  5. ps存jpeg,格式保存的时候为什么选择“基线”

    jpeg是印前和网页设计常用的格式,最大好处就是能很大程度上压缩图像大小. 在ps中将图片保存为jpeg格式的时候会出现以下选项: 其中:图像选项都很熟悉,是关于图像质量的:而格式选项的用途主要是针对 ...

  6. SALT-API兼HALITE测试搞定

    妈XX,真的搞了近一周的空闲时间. 最后才领悟. 其实,先按HALITE的套路弄好,然后直接SALT-API就OK了..因为HALITE就是SALT-API的封闭和替代嘛. 随便参考一个URL搞定HA ...

  7. spserver 开源服务器框架研究与分析

    网上开源的C/C++服务器框架 还是比较少的. 最近研究了 spserver , 里面用了较多的设计模式,使用设计模式的目的是把不变的东西和可变的东西分离并且封装起来,避免以后修改代码, 应用设计模式 ...

  8. JSP页面中文参数传递get和post方法分析

    原文 http://developer.51cto.com/art/200907/133499.htm 在项目中,我们经常遇到需要在JSP页面切换中传递中文字符.这主要有两种方式. ◆URL方式 例如 ...

  9. 操作系统对的IIS版本

    IIS版本 Windows版本 备注 IIS 1.0 Windows NT 3.51 Service Pack 3   IIS 2.0 Windows NT 4.0   IIS 3.0 Windows ...

  10. 【Android】使用BaseAdapter实现复杂的ListView

    步骤 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你要准备好 List getData(). 2. 继承ListActivity专有屏,不再需要setContentView ...