Longest Common Substring

SPOJ - LCS

题意:求两个串的最长公共子串

/*
对一个串建立后缀自动机,用另一个串上去匹配
*/
#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的更多相关文章

  1. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  2. spoj - Longest Common Substring(后缀自动机模板题)

    Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...

  3. SPOJ Longest Common Substring II

    题目连接:戳我 题目大意:求n个字符串的最长公共子串. 它的简化版--这里 当然我们可以用SA写qwq,也可以用广义SAM写qwq 这里介绍纯SAM的写法...就是对其中一个建立后缀自动机,然后剩下的 ...

  4. 2018.12.15 spoj Longest Common Substring II(后缀自动机)

    传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...

  5. spoj Longest Common Substring (多串求最大公共子序列)

    题目链接: https://vjudge.net/problem/SPOJ-LCS 题意: 最多10行字符串 求最大公共子序列 数据范围: $1\leq |S| \leq100000$ 分析: 让他们 ...

  6. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

  7. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  8. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  9. SPOJ 10570 LONGCS - Longest Common Substring

    思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成多组数据就有三倍经验了 代码 #include <cstdio> #inclu ...

随机推荐

  1. springboot实现定时任务的一种方式

    参考:https://www.yoodb.com/news/detail/1205 目前觉得这种还是很好用,所以就记录. 最好新建一个项目测试: 1. pom中添加 <dependency> ...

  2. Day3(2)bash的特性

    bash的基础特性: (1)命令历史 history 环境变量: HISTSIZE:命令零食记录的条数: HISTFILE:~/.bash_history: HISFILESIZE:命令历史文件记录历 ...

  3. 有关UCOS_II在LPC1768上的应用

    https://www.cnblogs.com/chungshu/archive/2012/12/14/2818380.html

  4. WebDriver数据驱动模式

    利用@dataprovider 在一个浏览器内多次登录不同的用户时,必须要每次完成一个登录后,都有一个退出登录的代码,以保持和初始登录页面一致,才不会报错并再次循环登录

  5. Codeforce 101B. Buses(线段树or树状数组+离散化)

     Buses                                                                                               ...

  6. 第三章 Java程序优化(待续)

    字符串优化处理 String对象及其特点 String对象是java语言中重要的数据类型,但它并不是Java的基本数据类型.在C语言中,对字符串的处理最通常的做法是使用char数组,但这种方式的弊端是 ...

  7. QT5提示can not find -lGL的解决方法

    这是由于 Qt5.0 默认将OpenGL加入了工程,但是在机器上没有安装OpenGL,所以jonas只需要在机器上安装OpenGL即可 .   安装建立基本编译环境 首先不可或缺的,就是编译器与基本的 ...

  8. java 多线程系列---JUC原子类(三)之AtomicLongArray原子类

    AtomicLongArray介绍和函数列表 在"Java多线程系列--“JUC原子类”02之 AtomicLong原子类"中介绍过,AtomicLong是作用是对长整形进行原子操 ...

  9. 部署和调优 1.1 nfs部署和优化-2

    更改共享目录文件默认的所有者和所属组 已知道客户端有个user11用户 cat /etc/passwd user11:x:501:501::/home/user11:/bin/bash 服务端打开 v ...

  10. Javascript 面向对象(一):封装

    Javascript 面向对象编程(一):封装 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言, ...