题目大意:

给出两个长度小于等于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. unique &unique_copy

      unique (ForwardIterator first, ForwardIterator last); unique (ForwardIterator first, ForwardIterat ...

  2. PAT 65. A+B and C (64bit) (20)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1065 思路分析: 1)对a+b造成的long long 类型的数据溢出进行特殊处理: a> ...

  3. org.hibernate.service.jndi.JndiException: Error parsing JNDI name []

    我的hibernate.cfg.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTY ...

  4. angularjs directive 实例 详解

    前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...

  5. Scala学习笔记--上界(<:)、视图界定(<%)、上下文界定(T:M)

    上界 下界 视界 object Test{ def main(args:Array[String]):Unit={ def mulBy(factor:Double)=(x:Double)=>fa ...

  6. js判断输入的是单字节还是双字节

    计算机上每一个字符.数字.汉字.字母等,都对应着unicode编码,unicode编码小于255的为单字节,大于255.小于65535的为双子节或三字节 查看一个字符对应的unicode编码 var ...

  7. CocosCode IDE 打包APK失败的原因

    因为在lua目录下应用了git版本控制,导致打包的时候,复制(移动).git里面的文失败.从而第一次能打包成功,第二次之后就会失败.提示 os.move()失败.对应framworks/runtime ...

  8. PowerShell 远程管理之 about_Remote_Troubleshooting

    主题 about_Remote_Troubleshooting 简短说明 说明如何解决 Windows PowerShell 中的远程操作的问题. 详细说明 本节描述您在使用 Windows Powe ...

  9. GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&tip=0

    GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&am ...

  10. CentOS下安装无线网卡驱动 (转)

    1. 确定自己的网卡和内核版本:lspci | grep Network  #根据输出的信息确定网卡的型号.uname -a             #确定内核版本 2. 配置yum使用RPMForg ...