题目链接: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. 论JSON的重要性☞异步上传过程中data取多组值的方法

    异步上传取多组值一起post改变属性 如图: 看图说话

  2. C#文件处理

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. HttpURLConnection传JSON数据

    try { //创建连接 URL url = new URL(url); HttpURLConnection connection = (HttpURLConnection) url.openConn ...

  4. H5 App页面 绝对定位 软键盘弹出时顶起底部按钮

    做H5 App页面时,有时候,按钮可能会放到页面的最底下,这个时候可能会用到绝对定位(position: absolute),但是,当input 输入框被点击时,弹出的软键盘会顶起底部的按钮,就像这样 ...

  5. B+树概念学习

    转载自 从B树.B+树.B*树谈到R 树 1.用阶定义的B树 B 树又叫平衡多路查找树.一棵m阶的B 树 (注:切勿简单的认为一棵m阶的B树是m叉树,虽然存在四叉树,八叉树,KD树,及vp/R树/R* ...

  6. 那些年,我们被耍过的bug——haslayout

    你被IE的bug耍过几次了? IE,这个令所有网站设计人员讨厌,但又不得不为它工作的浏览器.不论是6.7还是8,它们都有一个共同的渲染标准haslayout,所以haslayout 是一个非常有必要彻 ...

  7. 百度移动搜索自动转码太坑爹,JS跳转地址会被抓取

    这段时间碰到个很崩溃的问题,一个页面通过 script 加载请求服务端进行统计再输出js进行跳转,分为两个步骤分别统计, 打开页面通过script 请求远程服务器进行统计并输出要通过js使页面跳转的最 ...

  8. GrowingIO 2016 数据驱动增长大会—— 一起做增长英雄

    GrowingIO 2016 数据驱动增长大会,首次聚齐了增长黑客之父 Sean Ellis .世界前十位前沿数据科学家张溪梦等数十位中美顶尖增长实践者: 链家.点融网.Camera360.量化派.北 ...

  9. Spring Security(19)——对Acl的支持

    目录 1.1           准备工作 1.2           表功能介绍 1.2.1     表acl_sid 1.2.2     表acl_class 1.2.3     表acl_obj ...

  10. eclipse安装插件的4种方式

    Eclipse插件的安装方法大体有以下三种:[9] 第一种:直接复制法 假设Eclipse的安装目录在C:\eclipse,解压下载的eclipse 插件或者安装eclipse 插件到指定目录AA(如 ...