spoj Longest Common Substring
Longest Common Substring
题意:求两个串的最长公共子串
/*
对一个串建立后缀自动机,用另一个串上去匹配
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 250002
using namespace std;
char s1[maxn],s2[maxn];
int ch[maxn<<][],fa[maxn<<],tot=,len[maxn<<],last=,p,np,q,nq;
void Insert(int c){
np=++tot;
len[np]=len[last]+;
p=last;
while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
if(!p)fa[np]=;
else {
q=ch[p][c];
if(len[q]==len[p]+)fa[np]=q;
else {
nq=++tot;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
len[nq]=len[p]+;
while(ch[p][c]==q)ch[p][c]=nq,p=fa[p];
}
}
last=np;
}
void solve(){
int l=strlen(s2+),now=,ans=,now_len=,c;
for(int i=;i<=l;i++){
c=s2[i]-'a';
while(now&&!ch[now][c]){
now=fa[now];
now_len=len[now];
}
if(!now){
now_len=;
now=;
}
else if(ch[now][c]){
now_len++;
now=ch[now][c];
ans=max(ans,now_len);
}
}
printf("%d",ans);
}
int main(){
scanf("%s%s",s1+,s2+);
int l=strlen(s1+);
for(int i=;i<=l;i++)Insert(s1[i]-'a');
solve();
return ;
}
spoj Longest Common Substring的更多相关文章
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
- spoj - Longest Common Substring(后缀自动机模板题)
Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...
- SPOJ Longest Common Substring II
题目连接:戳我 题目大意:求n个字符串的最长公共子串. 它的简化版--这里 当然我们可以用SA写qwq,也可以用广义SAM写qwq 这里介绍纯SAM的写法...就是对其中一个建立后缀自动机,然后剩下的 ...
- 2018.12.15 spoj Longest Common Substring II(后缀自动机)
传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...
- spoj Longest Common Substring (多串求最大公共子序列)
题目链接: https://vjudge.net/problem/SPOJ-LCS 题意: 最多10行字符串 求最大公共子序列 数据范围: $1\leq |S| \leq100000$ 分析: 让他们 ...
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- SPOJ 10570 LONGCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成多组数据就有三倍经验了 代码 #include <cstdio> #inclu ...
随机推荐
- 2015.12.14 MDI(多文档窗口结构)设置基本解决,折腾一天,部分解决存在已久的问题。但效果仍不如临时航线的MDI窗体结构。
创建从一个窗口弹出多个子窗口的结构叫MDI窗体结构 如果不按MDI结构管理,最简单的做法是: 在窗体A上添加菜单或按钮,在菜单或按钮事件中添加弹出B窗体代码: B b = new B(); b.sho ...
- xcopy 命令行
https://www.cnblogs.com/yang-hao/p/6003308.html xcopy-参数详解 XCOPY——目录复制命令 1.功能:复制指定的目录和目录下的所有文件连同目 ...
- doker 笔记(1) 架构
Docker 的核心组件包括: Docker 客户端 - Client Docker 服务器 - Docker daemon Docker 镜像 - Image Registry Docker 容器 ...
- LAMP 2.4 Apache访问控制
通过查看日志发现有个IP 恶意攻击你的网址,可以控制这个IP的访问. 打开主配置文件复制模板. vim /usr/local/apache2/conf/httpd.conf 搜索 /Order 复制 ...
- 斐波那契数列-java实现
1,1,2,3,5,8,13,21...... 以上的数列叫斐波那契数列,今天的面试第一题,输出前50个,这里记录下. 方式一 package com.geenk.demo.my; /** * @au ...
- DevTools in Spring Boot 1.3
Spring Boot 1.3 will ship with a brand new module called spring-boot-devtools. The aim of this modul ...
- SQL语句兼容性规范
一.DDL兼容性规范(防止表结构变更后,原有的SQL执行报错)只能增加字段或修改字段长度(字段长度改大),不能修改字段名字和类型,不能删除字段不能删除表或者修改表名称 二.DML兼容性规范insert ...
- SQL serve 数据库--视图、事物、分离附加、备份还原
视图是数据库中的一种虚拟表,与真实的表一样,视图包含一系列带有名称的行和列数据.行和列数据用来自定义视图的查询所引用的表,并且在引用视图时动态生成. 视图只能用来查询,不能增删改:不允许出现重复列 ...
- ROS Learning-031 (提高篇-009 A Mobile Base-07) 控制移动平台 --- (操作)人机交互
ROS 提高篇 之 A Mobile Base-07 - 控制移动平台 - (操作)人机交互 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14 ...
- Luogu 3759 [TJOI2017]不勤劳的图书管理员
再也不作死写FhqTreap作内层树了,卡的不如暴力呜呜呜…… 题意翻译:给一个序列,每个下标包含两个属性$a$和$v$,求第一个属性与下标形成的所有逆序对的第二个属性和,给出$m$个交换两个下标的操 ...