codevs 3160 最长公共子串(SAM)
3160 最长公共子串
给出两个由小写字母组成的字符串,求它们的最长公共子串的长度。
读入两个字符串
输出最长公共子串的长度
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
27
单个字符串的长度不超过100000
【思路】
SAM求LCS
【代码】
#include<cstdio>
#include<cstring>
using namespace std; const int N = *1e5+; char s[N];
int sz,root,last,fa[N],ch[N][],l[N],n; void add(int x) {
int c=s[x]-'a';
int p=last,np=++sz; last=np;
l[np]=x+;
for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if(!p) fa[np]=root;
else {
int q=ch[p][c];
if(l[p]+==l[q]) fa[np]=q;
else {
int nq=++sz; l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
for(;p&&q==ch[p][c];p=fa[p]) ch[p][c]=nq;
}
}
}
void build() {
root=last=++sz;
scanf("%s",s);
n=strlen(s);
for(int i=;i<n;i++) add(i);
}
void lcs() {
scanf("%s",s);
n=strlen(s);
int p=root,ans=,len=;
for(int i=;i<n;i++) {
int c=s[i]-'a';
if(ch[p][c]) { len++; p=ch[p][c]; }
else {
for(;p&&!ch[p][c];p=fa[p]);
if(!p) { p=root; len=; }
else {
len=l[p]+; p=ch[p][c];
}
}
if(len>ans) ans=len;
}
printf("%d",ans);
} int main() {
build();
lcs();
return ;
}
codevs 3160 最长公共子串(SAM)的更多相关文章
- codevs 3160 最长公共子串
3160 最长公共子串 http://codevs.cn/problem/3160/ 时间限制: 2 s 空间限制: 128000 KB 题目描述 Description 给出两个由小写字母组 ...
- Codevs 3160 最长公共子串(后缀数组)
3160 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...
- codevs 3160 最长公共子串 后缀自动机
http://codevs.cn/problem/3160/ 后缀自动机板子题,匹配的时候要注意如果到一个点失配向前匹配到一个点时,此时的tmp(当前匹配值)为t[j].len+1而不是t[t[j]. ...
- CODE【VS】3160 最长公共子串 (后缀自动机)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...
- CODE【VS】 3160 最长公共子串 (后缀数组)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...
- 【wikioi】3160 最长公共子串(后缀自动机)
http://codevs.cn/problem/3160/ sam的裸题...(之前写了spoj上另一题sam的题目,但是spoj被卡评测现在还没评测完QAQ打算写那题题解时再来详细介绍sam的.. ...
- Codevs 1425 最长公共子串
1425 最长公共子串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Description 输入N(2<=N<= ...
- 【SAM】codevs3160-最长公共子串
[题目大意] 求两个字符串的最长公共子串. [思路] 对第一个字符串建立后缀自动机,第二个字符串去匹配.cnt记录当前最长公共子串的长度,而ret记录答案. p代表位置指针,初始在rt位置. 对于第二 ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
随机推荐
- 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...
- 创建Mysql
CREATE DATABASE IF NOT EXISTS yiya DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- LocalContainerEntityManagerFactoryBean
http://doc.okbase.net/liuyitian/archive/109276.html
- 软件测试 -- 和用户共同测试(UAT测试)的注意点有哪些
软件产品在投产前,通常都会进行用户验收测试.如果用户验收测试没有通过,直接结果就是那不到“Money”,间接影响是损害了公司的形象,而后者的影响往往更严重.根据作者的经验,用户验收测试一定要让用户满意 ...
- C++函数转换成C#函数
/// /// </param> /// <returns></returns> ...
- Automotive Security的一些资料和心得(5):Privacy
1. Introduction 1.1 "Customers own their data and we can be no more than the trsted stewards of ...
- 分别取商和余数:divmod(a, b)
使用函数:divmod(a, b)可以实现分别取商和余数的操作: >>> divmod(123,3) (41, 0) >>> divmod(200,6) (33, ...
- POJ 1236 Network of Schools[连通分量]
题目链接:http://poj.org/problem?id=1236题目大意:给出N台电脑,电脑间单向连通传送文件问题1.网络中最少放几个文件保证所有电脑都能接受到文件问题2.最少向网络中加几条线保 ...
- hdu 1423
最长公共上升子序列:O(n*m)的算法: #include<cstdio> #include<cstring> #define maxn 1000 using namespac ...
- WP8教程
http://www.maiziedu.com/courses-list?technology_category=6