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 ...
随机推荐
- MySQL中的事务
MySQL中的事务性: MySQL的InnoDB引擎是支持事务性的,事务是由多条SQL语句组成,是一个连续的一组数据库操作.只有该组内的每一个操作都成功时,整个事务才执行成功.(例如银行转账操作,只有 ...
- 手动为maven的本地仓库添加JAR
在要添加的jar所在的文件夹下打开黑屏 如添加Oracle的ojdbc6.jar 输入: mvn install:install-file -DgroupId=com.oracle -Dartifac ...
- css3 box-reflect 倒影效果
语法: box-reflect:包括3个值. 1. direction 定义方向,取值包括 above . below . left . right. above: 指定倒影在对象的上边 below: ...
- Java代理模式汇总
简介 代理模式即Proxy Pattern,23种java常用设计模式之一.其定义为:对其他对象提供一种代理以控制对这个对象的访问. UML类图 静态代理 目标接口 public interface ...
- 分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群(转载-2)
原文:http://www.cnblogs.com/PurpleDream/p/4510279.html 分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群 前言: ...
- JavaScriptSerializer返回一条Json,页面获取值问题,数据绑定
一,后台处理数据方法 JavaScriptSerializer serializer = new JavaScriptSerializer(); string js = serializer.Seri ...
- Android_AndroidStudio配置
IDE降低了程序编译的门槛, 让Android程序的编译和运行变得简单易操作. 但无论Eclipse还是Android Studio, IDE都不是非常智能和可靠的, 总会出大大小小的问题. 很多时候 ...
- 调皮的R文件,卑鄙的空格
毕业快一年了,由于公司业务需要,开发岗的我做了一年测试.最近,终于要开始转开发了.于是和小伙伴们合作,做一个备忘录apk.由于之前是做java的,而且差不多一年没碰代码了(这一年主要做测试,虽然有写自 ...
- 注册 Gmail,验证手机号码的时候提示“此号码不能用于验证”,怎么解决?
地址:https://www.zhihu.com/question/34834773 刚刚注册成功.折腾了好久,最后换了Chrome浏览器就成功了.手机号在注册的第一个界面时就填+86 xxxxx ...
- 织梦dedecms文章发布日期时间调用标签大全
dedecms首页时间标签: 1.12-27 样式 [field:pubdate function='strftime("%m-%d",@me)'/] 2.May 15, 2012 ...