题意:给定两个仅含小写字母的字符串,求他们最长公共子串的长度

n<=250000

思路:

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef pair<ll,int>P;
#define N 510000
#define M 151000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const int MOD=,inv2=(MOD+)/;
double eps=1e-;
ll INF=1e18;
ll inf=5e13;
int dx[]={-,,,};
int dy[]={,,-,}; char s[N];
int n,i,x,p,q,np,nq,cnt,L,st[N],c[N][],f[N],pos[N],bl[N],to[N],b[N],sz[N]; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void add(int x)
{
p=np;
st[np=++cnt]=st[p]+;
to[np]=i;
pos[i]=np;
for(;p&&!c[p][x];p=f[p]) c[p][x]=np;
if(!p) f[np]=;
else if(st[p]+==st[q=c[p][x]]) f[np]=q;
else
{
st[nq=++cnt]=st[p]+;
memcpy(c[nq],c[q],sizeof c[q]);
f[nq]=f[q];
f[q]=f[np]=nq;
for(;p&&c[p][x]==q;p=f[p]) c[p][x]=nq;
}
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
np=cnt=;
scanf("%s",s);
n=strlen(s);
rep(i,,n-) add(s[i]-'a'); rep(i,,cnt) b[st[i]]++;
rep(i,,n) b[i]+=b[i-];
rep(i,,cnt) bl[b[st[i]]--]=i;
for(i=,p=;i<n;i++) sz[p=c[p][s[i]-'a']]++;
for(i=cnt;i;i--) sz[f[bl[i]]]+=sz[bl[i]];
//rep(i,0,n-1) printf("%d ",sz[pos[i]]);
scanf("%s",s);
n=strlen(s);
int ans=;
for(p=,L=i=;i<n;i++)
{
if(c[p][x=s[i]-'a']) p=c[p][x],L++;
else
{
for(;!c[p][x]&&p;p=f[p]);
if(!p) p=,L=;
else L=st[p]+,p=c[p][x];
}
ans=max(ans,L);
}
printf("%d\n",ans);
return ;
}

【SPOJ1811】Longest Common Substring(后缀自动机)的更多相关文章

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

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

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

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

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

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

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

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

  5. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  6. spoj1811 Longest Common Substring

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

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

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

  8. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

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

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

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

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

随机推荐

  1. 125、TensorFlow计算图的执行

    # TensorFlow使用tf.Session类来表示客户端程序之间的链接 # 虽然一个在其他语言中相似的接口也是可以使用的,列如C++ runtime # 一个tf.Session对象提供了访问本 ...

  2. Js定义一个表单并提交

    Js定义一个表单 var form = $("<form>"); //定义一个form表单 form.attr('style', 'display:none'); // ...

  3. hihoCoder 1014 : Trie树(字典树)

    传送门 Description 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小 ...

  4. 微信小程序这一块(下)

    1.小程序分为两部分 :页面.组件 2.小程序中如何定义自定义组件? 凡是通过Page创建的都是页面,而通过Components创建的都是组件 当组件创建成功后,在需要使用的页面进行引入,(找到页面的 ...

  5. ES6 find 和 filter 的区别

    ES6 find 和 filter 的区别 : 遇到个功能是要分类就想说在前端过滤,不要从查数据库的时候过滤了.然后就想说除了filter还有啥好用的 发现有个find,测试一番之后发现 const ...

  6. 深入理解DiscoveryClient

    Spring Cloud Commons 提供的抽象 最早的时候服务发现注册都是通过DiscoveryClient来实现的,随着版本变迁把DiscoveryClient服务注册抽离出来变成了Servi ...

  7. Codeforces 1156E Special Segments of Permutation(单调栈)

    可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...

  8. 2019-1-5-Windows-的-Pen-协议

    title author date CreateTime categories Windows 的 Pen 协议 lindexi 2019-01-05 11:14:49 +0800 2019-01-0 ...

  9. mysql02---客户端与服务器模型

    目录 一.客户端与服务器模型 连接MySQL方式 总结: 二.MySQL服务器构成 三.MySQL的结构 一.客户端与服务器模型 1.mysql是一个典型的C/S服务结构 1.1 mysql自带的客户 ...

  10. txt_to_csv

    import csv def txt2csv(inf, outf): with open(inf,'r') as fin, open(outf,'w',newline='') as fout: wrt ...