题链:

http://poj.org/problem?id=2774

题解:

求两个字符串(S,T)的最长公共子串。
对 S串建后缀自动机。
接下来就用这个自动机去求出能和 S串匹配的 T的每一个前缀的最长的后缀。
最终答案就是对每个 T的前缀得到的答案取最大值就好了。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 250050
#define filein(x) freopen(#x".in","r",stdin);
#define fileout(x) freopen(#x".out","w",stdout);
using namespace std;
struct SAM{
int size,last,q,p,nq,np;
int pre[MAXN*2],step[MAXN*2],ch[MAXN*2][26];
int Newnode(int a,int b){
step[size]=a; memcpy(ch[size],ch[b],sizeof(ch[b]));
return size++;
}
void Extend(int x){
p=last; last=np=Newnode(step[p]+1,0);
while(p&&!ch[p][x]) ch[p][x]=np,p=pre[p];
if(!p) pre[np]=1;
else{
q=ch[p][x];
if(step[q]!=step[p]+1){
nq=Newnode(step[p]+1,q);
pre[nq]=pre[q]; pre[q]=pre[np]=nq;
while(p&&ch[p][x]==q) ch[p][x]=nq,p=pre[p];
}
else pre[np]=q;
}
}
void Build(char *S){
memset(ch[0],0,sizeof(ch[0]));
size=1; last=Newnode(0,0); pre[last]=0;
for(int i=0;S[i];i++) Extend(S[i]-'a');
}
int Match(char *T){
p=1; int ans=0;
for(int j=0,x,match=0;T[j];j++){ //与 T的每个前缀匹配
x=T[j]-'a';
if(ch[p][x]) match++,p=ch[p][x];
else{
while(p&&!ch[p][x]) p=pre[p];
if(!p) match=0,p=1;
else match=step[p]+1,p=ch[p][x];
}
ans=max(ans,match);
}
return ans;
}
}suf;
char S[MAXN],T[MAXN];
int main()
{
scanf(" %s %s",S,T);
suf.Build(S);
printf("%d",suf.Match(T));
return 0;
}

●SPOJ 1811 Longest Common Substring的更多相关文章

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

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

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

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

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

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

  4. SPOJ 1811 Longest Common Substring

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

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

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

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

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

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

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

  8. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  9. 【SPOJ】Longest Common Substring

    [SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...

随机推荐

  1. 随机ID添加

    var http = require("http"); var fs = require("fs"); var server = http.createServ ...

  2. bzoj千题计划275:bzoj4817: [Sdoi2017]树点涂色

    http://www.lydsy.com/JudgeOnline/problem.php?id=4817 lct+线段树+dfs序 操作1:access 操作2:u到根的-v到根的-lca到根的*2+ ...

  3. ThreadLocal就是这么简单

    前言 今天要研究的是ThreadLocal,这个我在一年前学习JavaWeb基础的时候接触过一次,当时在baidu搜出来的第一篇博文ThreadLocal,在评论下很多开发者认为那博主理解错误,给出了 ...

  4. django + nginx + uwsgi + websocket

    最近使用django框架做了一个简单的聊天机器人demo, 开发的过程中使用了django自带的websocket模块,当使用django框架自带的wsgi服务去启动的话,没有什么问题.如果要使用uw ...

  5. MSSQL 2000 错误823恢复

    一.故障描述 MSSQL Server 2000 附加数据库错误823,附加数据库失败.数据库没有备份,不能通过备份恢复数据库,急需恢复数据库中的数据. 二.故障分析SQL Server数据库 823 ...

  6. python之路--day8---day9--两日内容

    一.不使用函数的问题 1,代码的组织结构不清晰,可读性差 2,遇到重复的功能只能重复编写实现代码,代码冗余 3,功能需要扩展时,需要找出所有实现该功能的地方修改,无法统一管理且维护难度极大 二.函数是 ...

  7. 新概念英语(1-59)Is that all

    Does the lady buy any chalk? A:I want some envelopes, please. B:Do you want the large size or the sm ...

  8. C#微信公众号——本地调试

    测试微信,因为要与微信服务器进行交互,所以必须要是外网地址,实现本地调试首先需要解决的问题就是外网问题,这个我前面的文章有介绍,这里就不再详细介绍了,网址http://www.cnblogs.com/ ...

  9. leetcode算法: Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  10. ActiveMQ学习系列(三)----下载github源码并编译

    前记:坚持使用官网的资源去学习是挺痛苦的一个过程,昨天瞎溜达了一天,也没看到有系统性的学习文章,倒是发现了github上的ActiveMq项目. 地址:https://github.com/apach ...