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. 【转】 Pro Android学习笔记(八七):了解Handler(1):组件和线程

    目录(?)[-] 主线程消息队列和线程池 查看线程信息 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingf ...

  2. 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...

  3. MySQL 更新失效

    create table t(id int not null PRIMARY key,c int default null) engine=innodb;insert into t(id,c)valu ...

  4. mycat接oracle和mysql多个实例

    Mycat 后面接一个Oracle实例与一个MySQL实例,假设用户表,订单表,转账记录表,Oracle字符集为GBK的,MySQL字符集则要求UTF8的完成用户表与订单表到MySQL的迁移过程,要求 ...

  5. STL容器迭代器失效问题讨论

    STL源码剖析---迭代器失效小结 vector迭代器的几种失效的情况: .当插入(push_back)一个元素后,end操作返回的迭代器肯定失效. .当插入(push_back)一个元素后,capa ...

  6. oracle connect by用法篇 (包括树遍历)之二

    3.2查询当前时间往前的12周的开始时间.结束时间.第多少周 , ) - (rownum ) as startDate, sysdate , 'd'))) - (rownum ) as endDate ...

  7. c:if标签数据回显判断是否选中

    <form action="/brand/list.do" method="post" style="padding-top:5px;" ...

  8. 控制器对应view生命周期

    一.控制器view创建的六种方式 1.有没有同名xib创建2.通过 storyboard 创建3.有指定xib情况下创建4.有同名xib情况5.有同名去掉controll的情况6.loadveiw   ...

  9. C#正则表达式匹配双引号

    html: <img class="bubble large" src="/images/hero-logos/cog.svg" width=" ...

  10. Node.js的__dirname,__filename,process.cwd(),./的含义

    简单说一下这几个路径的意思,: __dirname: 获得当前执行文件所在目录的完整目录名 __filename: 获得当前执行文件的带有完整绝对路径的文件名 process.cwd():获得当前执行 ...