http://codevs.cn/problem/3160/

sam的裸题。。。(之前写了spoj上另一题sam的题目,但是spoj被卡评测现在还没评测完QAQ打算写那题题解时再来详细介绍sam的。。。。那就再等等吧。

求两个串的lcs话,就是先建立a串的sam,然后用b的字串去匹配a中。

因为sam中的转移可以直接对应所有后缀的开头,因此匹配的时候是可以直接找到这个后缀开头,然后继续转移,直至找到整个串。而因为sam中的parent指针就如ac自动机中的fail指针差不多,唯一的区别是sam的parent指针转移到的节点是自己的后缀(就是S到当前节点就是一个原串的前缀,而上面说的就是这个前缀的后缀,而且长度每次都递减,因此能找到最大匹配)

所以不断找然后更新即可。

如果还不懂详看clj论文

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } struct sam {
static const int N=200005;
int c[N][26], l[N], f[N], root, last, cnt;
sam() { cnt=0; root=last=++cnt; }
void add(int x) {
int now=last, a=++cnt; last=a;
l[a]=l[now]+1;
for(; now && !c[now][x]; now=f[now]) c[now][x]=a;
if(!now) { f[a]=root; return; }
int q=c[now][x];
if(l[q]==l[now]+1) { f[a]=q; return; }
int b=++cnt;
memcpy(c[b], c[q], sizeof c[q]);
l[b]=l[now]+1;
f[b]=f[q];
f[q]=f[a]=b;
for(; now && c[now][x]==q; now=f[now]) c[now][x]=b;
}
void build(char *s) {
int len=strlen(s);
rep(i, len) add(s[i]-'a');
}
int find(char *s) {
int ret=0, len=strlen(s), now=1, t=0;
rep(i, len) {
int x=s[i]-'a';
if(c[now][x]) now=c[now][x], ++t;
else {
while(now && !c[now][x]) now=f[now];
if(!now) now=root, t=0;
else t=l[now]+1, now=c[now][x];
}
ret=max(ret, t);
}
return ret;
}
}a;
const int N=100005;
char s[N];
int main() {
scanf("%s", s);
a.build(s);
scanf("%s", s);
printf("%d\n", a.find(s));
return 0;
}

  


题目描述 Description

给出两个由小写字母组成的字符串,求它们的最长公共子串的长度。

输入描述 Input Description

读入两个字符串

输出描述 Output Description

输出最长公共子串的长度

样例输入 Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
样例输出 Sample Output

27

数据范围及提示 Data Size & Hint

单个字符串的长度不超过100000

【wikioi】3160 最长公共子串(后缀自动机)的更多相关文章

  1. codevs 3160 最长公共子串 后缀自动机

    http://codevs.cn/problem/3160/ 后缀自动机板子题,匹配的时候要注意如果到一个点失配向前匹配到一个点时,此时的tmp(当前匹配值)为t[j].len+1而不是t[t[j]. ...

  2. CODE【VS】 3160 最长公共子串 (后缀数组)

    3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...

  3. Codevs 3160 最长公共子串(后缀数组)

    3160 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...

  4. SCOJ 4493: DNA 最长公共子串 后缀自动机

    4493: DNA 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4493 Description Deoxyribonucleic acid ( ...

  5. CODE【VS】3160 最长公共子串 (后缀自动机)

    3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...

  6. codevs 3160 最长公共子串

    3160 最长公共子串 http://codevs.cn/problem/3160/  时间限制: 2 s  空间限制: 128000 KB   题目描述 Description 给出两个由小写字母组 ...

  7. codevs 3160 最长公共子串(SAM)

    3160 最长公共子串   题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Ou ...

  8. poj 2774 最长公共子串 后缀数组

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10 ...

  9. bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)

    bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...

随机推荐

  1. 如何打印加密的PDF文件?

    如何打印加密的PDF文件? Pdf加密了不让打印怎么办?? 下载Foxit PDF Editor以下是下载地址:http://www.orsoon.com/Soft/4865.html 用它打开加密的 ...

  2. Jquery重新学习之六[操作XML数据]

    上一章整理有关Jquery操作JSON格式数据,本章则是整理Jquery与XML的交互,因为XML简单易用及运用在很跨平台上的优点,所以项目运用Jquery操作XML还是比较常见:下面的代码来自Jqu ...

  3. Java之JVM调优案例分析与实战(2) - 集群间同步导致的内存溢出

    环境:一个基于B/S的MIS系统,硬件为两台2个CPU.8GB内存的HP小型机,服务器是WebLogic 9.2,每台机器启动了3个WebLogic实例,构成一个6个节点的亲合式集群. 说明:由于是亲 ...

  4. android 利用cmdline,将參数从preloader传递到kernel

    以定义參数 cus_param 为例.int型变量. 1. Preloader部分 Platform.h(mediatek\platform\[$platform]\preloader\src\dri ...

  5. windows合并文件夹窗口

      windows合并文件夹窗口 CreateTime--2017年7月26日16:28:14Author:Marydon 右击任务栏-->属性-->任务栏按钮选项-->选择“始终合 ...

  6. JDBC 关于大文本数据

    大文本数据Clob,在不同的数据库中类型名不一致,有的是text格式,有的是clob,还有其他一些格式   package test; import java.io.BufferedReader; i ...

  7. Adminer - 类似于phpMyAdmin的MySQL管理客户端

    Adminer是一个类似于phpMyAdmin的MySQL管理客户端.整个程序只有一个PHP文件,易于使用和安装.Adminer支持多语言(已自带11种翻译语言文件,可以按自己的需求翻译相应的语言). ...

  8. weblogic设置jvm参数

    http://www.quiee.com.cn/archives/592/ weblogic a) 编辑Weblogic Server启动脚本文件:BEA_HOME\user_projects\dom ...

  9. Linux 设置IP,gate, 以及自动获取IP的方法

    一.使用命令设置ubuntu的ip地址 1.修改配置文件blacklist.conf禁用IPV6: sudo vi /etc/modprobe.d/blacklist.conf 2.在文档最后添加 b ...

  10. spring in action 9.1 spring security

    spring security是基于spring AOP 和 Servlet 规范中的Filter 实现的安全框架. Spring Security 是为基于 Spring 的应用程序提供声明式安全保 ...