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

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. 像计算机科学家一样思考python-第2章 变量、表达式和语句

    感想: 1.程序出现语义错误时,画状态图是一个很好的调试办法.打印出关键变量在不同代码处理后值的变化,就能发现问题的蛛丝马迹. 2.每当学习新语言特性时,都应当在交互模式中进行尝试,并故意犯下错误,看 ...

  2. JavaScript浅析

    目录 JacaScript概述 ECMAScript和JavaScript的关系 ECMAScript的历史: JavaScript的引入方式: 引入额外的JS文件: JavaScript的语言规范: ...

  3. 安卓中java和js如何交互

    1.安卓中java和js如何交互 在Android上怎样实现JAVA和JS交互呢?Android的webview是基于webkit内核的,webview中集成了js与java互调的接口函数,通过add ...

  4. 【ABAP系列】SAP 的逻辑数据库解析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 的逻辑数据库解析   前 ...

  5. 写的一个双向链表,测试OK

    #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct DoubleLi ...

  6. Hibernate入门2

    实体类的编写规则 要求实体类的属性是私有的 要求实体类中的私有属性有公开的get和set方法(设置器和访问器) 要求实体类有一个属性作为唯一值(一般使用id值) 实体类属性建议不使用基本数据类型,使用 ...

  7. 《JAVA设计模式》之命令模式(Command)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述命令(Command)模式的: 命令模式属于对象的行为模式.命令模式又称为行动(Action)模式或交易(Transaction)模式. ...

  8. Python 根据入栈顺利判定出栈顺序

    1.读取入栈,出栈数据: 2.把数据分别转化成整数列表: 3.新建栈列表,用入栈数据进行压栈:如果栈列表不为空,并且栈顶层数据为出栈的元素:删除栈列表的顶层数据: 4.如果栈列表不为空,说明栈列表里面 ...

  9. LeetCode #657. Robot Return to Origin 机器人能否返回原点

    https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...

  10. 3、NumPy 数组属性

    1.秩.维度 NumPy 数组的维数称为秩(rank),一维数组的秩为 1,二维数组的秩为 2,以此类推. 在 NumPy中,每一个线性的数组称为是一个轴(axis),也就是维度(dimensions ...