题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=2796

把一个字符串做出后缀自动机,另一个字符串与之匹配。

 #include<cstdio>
#include<cstring>
#include<iostream>
#define inf 1<<30
#define maxn 250005
using namespace std;
int tot,last,root,ans,sum,n,m;
char s[maxn],c[maxn];
struct fuck{int par,go[],val;}suf[maxn*];
int newnode(int x){suf[++tot].val=suf[x].val+;return tot;}
void extend(int x){
int p=last,np=newnode(p);
while(p&&suf[p].go[x]==) suf[p].go[x]=np,p=suf[p].par;
if(p==) suf[np].par=root;
else{
int q=suf[p].go[x];
if(suf[q].val==suf[p].val+) suf[np].par=q;
else{
int nq=newnode(p);
memcpy(suf[nq].go,suf[q].go,sizeof(suf[q].go));
suf[nq].par=suf[q].par;
suf[q].par=suf[np].par=nq;
while(p&&suf[p].go[x]==q) suf[p].go[x]=nq,p=suf[p].par;
}
}
last=np;
}
int main(){
last=root=tot=; ans=;
scanf("%s%s",s,c);
n=strlen(s),m=strlen(c);
for(int i=;i<n;i++) extend(s[i]-'a');
for(int i=,pp=root;i<m;i++){
int f=c[i]-'a';
if(suf[pp].go[f]) sum++,pp=suf[pp].go[f];
else{
while(pp && !suf[pp].go[f]) pp=suf[pp].par;
if(pp) sum=suf[pp].val+,pp=suf[pp].go[f];
else sum=,pp=root;
}
ans=max(ans,sum);
}
printf("%d\n",ans);
return ;
}

spoj1811:Longest Common Substrin的更多相关文章

  1. spoj1811 Longest Common Substring

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

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

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

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

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

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

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

  5. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

  6. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  9. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

随机推荐

  1. Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码

    (从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...

  2. install plugin elasticsearch-analysis-ik

    1.github下载分词器插件(请各位下载自己elasticsearch相对应的版本,否则会有兼容性问题) https://github.com/medcl/elasticsearch-analysi ...

  3. 简单的HTML注册界面

    <html><head><title>我的第一个HTML页面</title></head><body><center> ...

  4. hdu 1907 John&& hdu 2509 Be the Winner(基础nim博弈)

    Problem Description Little John is playing very funny game with his younger brother. There is one bi ...

  5. YYKit笔记之FPS

    FPS计算方法 FPS是Frame per second的缩写,即每秒的帧数.这一术语广泛的应用于计算机图形学,视频采集,游戏等. CADisplayLink CADisplayLink是一个能让我们 ...

  6. android studio Activity标题栏研究

    第一次研究时间:2016/7/30,以下研究主要存在于当前最新版本的android studio上.eclipse请参考 一.头部标题取消 当前版本新建工程在 application中默认主题为 an ...

  7. ECStore图片存储采用阿里云OSS(图片存储)服务

    主要功能:ECStore图片存储采用阿里云OSS(图片存储)服务   适用版本:ECStore 授权方式:授权域名使用,付费插件 联系方式: QQ 275553385  mail: jimingson ...

  8. CodeForces 712D Memory and Scores

    $dp$,前缀和. 记$dp[i][j]$表示$i$轮结束之后,两人差值为$j$的方案数. 转移很容易想到,但是转移的复杂度是$O(2*k)$的,需要优化,观察一下可以发现可以用过前缀和来优化. 我把 ...

  9. 伪AP检测技术研究

    转载自:http://www.whitecell-club.org/?p=310 随着城市无线局域网热点在公共场所大规模的部署,无线局域网安全变得尤为突出和重要,其中伪AP钓鱼攻击是无线网络中严重的安 ...

  10. jqery总结