题目链接: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. 【锋利的Jquery】读书笔记五

    jquery表单 表格操作 表单从基本的得到和失去焦点表单验证 <script type="text/javascript"> $(function(){ $(&quo ...

  2. Racket中使用Y组合子

    关于Y组合子,网上已经介绍很多了,其作用主要是解决匿名lambda的递归调用自己. 首先我们来看直观的递归lambda定义, 假设要定义阶乘的lambda表达,C#中需要这么定义 Func<in ...

  3. Java学习笔记-File

    //文件操作 //2015年4月30日15:40:21 package com.alfredsun.first.main; import java.io.File; import java.io.IO ...

  4. 设计模式 -- 代理模式 (Proxy Pattern)

    定义: 为其他对象提供一种代理以控制对这个对象的访问: 角色: 1,抽象主题类,(接口或者抽象类),抽象真实主题和代理的共有方法(如下Subject类): 2,具体实现的主题类,继承或者实现抽象主题类 ...

  5. 敏捷开发(六)- SCRUM全员会议

    本文主要是为了检测你对SCRUM 全员会议的了解和使用程度,通过本文你可以检测一下  1.你们的SCRUM 全员会议的过程和步骤    2.SCRUM 全员会议的输出结果 一.会议目的     组成团 ...

  6. HTTP 错误 404.8 - Not Found

    HTTP 错误 404.8 - Not Found请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径. 详细错误信息模块 RequestFilteringModule 通 ...

  7. Laravel生成编译文件权限问题

    sudo -unobody ./artisan optimize # 避免`fpm`账户无权限修改缓存文件的问题.这里的`nobody`改成实际的`fpm`用户名.

  8. ES 6 : 变量的解构赋值

    1. 数组的解构赋值 [ 基本用法 ] 按照一定的模式从数组或者对象中取值,对变量进行赋值的过程称为解构. 以前,为变量赋值只能直接指定值: 而ES 6 允许写成下面这样: 上面的代码表示,可以从数组 ...

  9. 面试题-Java基础-垃圾回收

    1.Java中垃圾回收有什么目的?什么时候进行垃圾回收? 垃圾回收的目的是识别并且丢弃应用不再使用的对象来释放和重用资源. 2.System.gc()和Runtime.gc()会做什么事情? 这两个方 ...

  10. vscode: Visual Studio Code 常用快捷键

    vscode: Visual Studio Code 常用快捷键 主命令框 F1 或 Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以输入任何命令,例如: 按一下 Backspace ...