spoj1811:Longest Common Substrin
题目链接: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的更多相关文章
- spoj1811 Longest Common Substring
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
随机推荐
- U3D简单得换装技术
四个类完成,前提是 资源得名字配合 UI按钮点击响应类 using UnityEngine; using System.Collections; public class ButtonClickHan ...
- Python学习笔记基础篇——总览
Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...
- MATLAB初体验
好激动 要入MATLAB的大坑了 恩 很遗憾第一个程序并不是hello world 好 插入代码里并没有MATLAB这个选项 这是一种歧视 x=[:pi/:*pi]; y=sin(x); plot(x ...
- 工具-Quick time播放器
拍屏的视频.素材视频等,用Quick time观看 1.左下角可切换时间/帧编号,直观看到某pose的帧位置: 2.播放进度条上有卡尺,可选择部分视频,显示-仅播放所选部分,点上显示-循环,可以反复观 ...
- Redis同步操作失败的原因
今天弄了下 Redis 的主从同步,设置方法其实很简单的,但崩溃的是遇到个莫名其妙的问题,始终同步不了.. 看了看错误日志: Unable to connect to MASTER: Invalid ...
- CodeForces 711D Directed Roads
计数,模拟. 首先观察一下给出的图的特点: $1.$一定存在环. $2.$可能存在多个环. 我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种 ...
- java中的反射机制_____
一,先看一下反射的概念: 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义. 反射是java中一种强大的工具,能够使 ...
- eclipse中link方式安装插件
今天需要给eclipse安装svn插件,觉得link方式便于管理于是就打算用这种方式来安装. 我电脑上的eclipse的安装目录是 E:\tools\eclipse 下面开始安装 1.在ecl ...
- Java 并发 关键字volatile
Java 并发 关键字volatile @author ixenos volatile只是保证了共享变量的可见性,不保证同步操作的原子性 同步块 和 volatile 关键字机制 synchroniz ...
- C++类与static
到目前为止,我们设计的类中所有的成员变量和成员函数都是属于对象的,如我们在前面定义的book类,利用book类声明两个对象Alice和Harry,这两个对象均拥有各自的price和title成员变量, ...