后缀自动机简单题。

其主要思路是,先对第一个字符串建立后缀自动机,把第二个串放在上面匹配,

若当前状态s有字符x的转移,直接转移len=step+1。

若当前状态s没有向字符x的转移,退回pres检查是否有转移,

若没有,则继续退回,否则转移到节点y,len=stepy+1。

以上思路主要利用的是关于pre节点的性质,由于pre节点的right集合是其子节点的right集合的并集,且max(pre)=min(s)-1.

所以从pre节点往上跳的时候,就能找到最长的后缀相同的部分的节点。(不知道这么说对不对)。

与KMP算法的很像。

可以参考这篇博客

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define mem1(i,j) memset(i,j,sizeof(i))
#define mem2(i,j) memcpy(i,j,sizeof(i))
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "dealing"
#define poi vec
#define eps 1e-10
#define db double
const int maxn=,inf=,mod=;
int read(){
int x=,f=,ch=getchar();
while(x<''||x>''){if(ch=='-')f=-;ch=getchar();}
while(x<=''&&x>=''){x=(x<<)+(x<<)+ch-'',ch=getchar();}
return f*x;
}
bool cmax(int& a,int b){return a<b?a=b,true:false;}
namespace sam{
const int maxn=;
int pre[maxn],c[maxn][],len[maxn],now,cnt,np,p,q,nq,Len;
void clear(){mem1(c,);mem1(len,);mem1(pre,);cnt=,now=;}
void expend(int x){
np=++cnt;len[np]=len[now]+;p=now;now=np;
while(p&&!c[p][x])c[p][x]=np,p=pre[p];
if(!p)pre[np]=;
else {
q=c[p][x];
if(len[q]==len[p]+)pre[np]=q;
else {
len[nq=++cnt]=len[p]+;
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
mem2(c[nq],c[q]);
while(p&&c[p][x]==q)c[p][x]=nq,p=pre[p];
}
}
}
int walk(int x){
while(!c[now][x]&&pre[now])now=pre[now],Len=len[now];
if(!c[now][x])return ;
now=c[now][x];Len++;return Len;
}
void build(char* s){
int n=strlen(s+);
up(i,,n)expend(s[i]-'a');
}
};
char s[maxn];
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
sam::clear();
scanf("%s",s+);
sam::build(s);
scanf("%s",s+);
int n=strlen(s+);
int ans=;sam::now=;sam::Len=;
up(i,,n)cmax(ans,sam::walk(s[i]-'a'));
cout<<ans<<endl;
return ;
}

SPOJ1811 LCS SAM的更多相关文章

  1. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

  2. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  3. SAM初探

    SAM,即Suffix Automaton,后缀自动机. 关于字符串有很多玩法,有很多算法都是围绕字符串展开的.为什么?我的理解是:相较于数字组成的序列,字母组成的序列中每个单位上元素的个数是有限的. ...

  4. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

  5. 后缀自动机(SAM) 学习笔记

    最近学了SAM已经SAM的比较简单的应用,SAM确实不好理解呀,记录一下. 这里提一下后缀自动机比较重要的性质: 1,SAM的点数和边数都是O(n)级别的,但是空间开两倍. 2,SAM每个结点代表一个 ...

  6. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

  7. BZOJ 4698: Sdoi2008 Sandy的卡片 [后缀自动机]

    4698: Sdoi2008 Sandy的卡片 题意:差分后就是多个串LCS SAM+map大法好 模板打错 智力-2 #include <iostream> #include <c ...

  8. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  9. 【spoj1811 & spoj1812 - LCS1 & LCS2】sam

    spoj1811  给两个长度小于100000的字符串 A 和 B,求出他们的最长公共连续子串. 先将串 A 构造为 SAM ,然后用 B 按如下规则去跑自动机.用一个变量 lcs 记录当前的最长公共 ...

随机推荐

  1. BZOJ3751 NOIP2014 解方程(Hash)

    题目链接  BZOJ3751 这道题的关键就是选取取模的质数. 我选了4个大概几万的质数,这样刚好不会T 然后统计答案的时候如果对于当前质数,产生了一个解. 那么对于那些对这个质数取模结果为这个数的数 ...

  2. SRM1154--Topcoder初体验

    SRM 711 DIV2 <br > 在frank_c1的帮助下,辣鸡Xiejiadong也开始做Topcoder辣...... <br > 这算是一次Topcoder的初体验 ...

  3. java常用IO

    字节流:FileInputStream.FileOutputStream 字符流:FileWriter.FileReader 转换流:InputStreamReader.OutputStreamRea ...

  4. Leetcode总结之Tree

    package Tree; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; imp ...

  5. P2P技术简介(包括BT软件的分析)(转)

    这是一篇别人发表的论文,里面很全面的解释了P2P技术的实现,以及BT网络中应用P2P技术所设计的原理,并列举BT软件的一些专业名词的定义.由于论文发表的比较早,2005年时还没有DHT技术. (链接: ...

  6. 如何使用ssh远程编辑定时任务crontab?

    linxu定时任务使用crontab,编辑crontab可以直接编辑:crontab -e:也可以直接读取文件 crontab file.这两种操作都不需要特殊权限sudo.区别在于,crontab ...

  7. HttpClient获取Cookie的两种方式

    转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...

  8. sql server 2008出现远程过程调用失败

    sql server  2008出现远程过程调用失败解决方式有下面几种: 1.依照网上说的方法.是由于装了vs 2012或是vs2013或是vs2015等高版本号时.安装了Microsoft SQL ...

  9. spring secrity 一些常用小知识

    1.在JSP页面获取当前登录的用户名的方法 首先引入taglib:<%@ taglib prefix="sec" uri="http://www.springfra ...

  10. Sales Team 仪表盘

                实际设定值         仪表定义     <div class="oe_center" t-if="record.invoiced_ta ...