博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SPOJ 1812 Longest Common Substring II
阅读量:7069 次
发布时间:2019-06-28

本文共 2317 字,大约阅读时间需要 7 分钟。

 

题意:

求10个串的LCS

 

1、用第一个串建立后缀自动机

2、len[s] 表示状态s 所能代表的字符串的最大长度

      mx[s] 表示状态s 在 当前匹配的串的最长匹配后缀长度

      ans[s] 表示状态s 在所有串的最长匹配后缀长度

3、用第2——第10个串在后缀自动机上跑,每次mx[s]=max(mx[s],当前匹配长度)

      每一个串跑完之后,更新 ans[s]=min(ans[s],mx[s])

4、每次匹配完一个字符串的时候,要 从后缀自动机 parent 树 上的叶子节点 向根更新,

   因为后缀自动机的parent树上,min[s]=max(fa[s])+1,所以子节点能匹配的长度 比 父节点的max要长。父节点是子节点的后缀,父节点可以匹配子节点的后max(fa[s])位,但是在那串在后缀自动机上跑的时候,不能保证经过 s 的 时候 也经过了s到根的链。所以只要子节点s 有匹配长度,父节点的mx[fa[s]]即可修改为len[fa[s]]即max(fa[s])

 

#include
#include
#include
using namespace std;#define N 100002char s[N];int tot=1,len[N<<1],ch[N<<1][26],fa[N<<1];int last=1,p,q,np,nq;int c[N],sa[N<<1];int mx[N<<1],ans[N<<1];void extend(int c){ len[np=++tot]=len[last]+1; ans[tot]=len[tot]; for(p=last;p && !ch[p][c];p=fa[p]) ch[p][c]=np; if(!p) fa[np]=1; else { q=ch[p][c]; if(len[q]==len[p]+1) fa[np]=q; else { nq=++tot; memcpy(ch[nq],ch[q],sizeof(ch[q])); fa[nq]=fa[q]; fa[q]=fa[np]=nq; ans[nq]=len[nq]=len[p]+1; for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq; } } last=np;}void build(){ scanf("%s",s+1); int L=strlen(s+1); for(int i=1;i<=L;++i) extend(s[i]-'a'); for(int i=1;i<=tot;++i) c[len[i]]++; for(int i=1;i<=L;++i) c[i]+=c[i-1]; for(int i=tot;i;--i) sa[c[len[i]]--]=i;}void solve(){ int L,c,now,now_len; int x; while(scanf("%s",s+1)!=EOF) { L=strlen(s+1); now=1; now_len=0; for(int i=1;i<=L;++i) { c=s[i]-'a'; while(now && !ch[now][c]) { now=fa[now]; now_len=len[now]; } if(!now) { now_len=0; now=1; } else if(ch[now][c]) { now_len++; now=ch[now][c]; } mx[now]=max(mx[now],now_len); } for(int i=tot;i;--i) { x=sa[i]; ans[x]=min(ans[x],mx[x]); if(fa[x] && mx[x]) mx[fa[x]]=len[fa[x]]; mx[x]=0; } } int out=0; for(int i=1;i<=tot;++i) out=max(out,ans[i]); printf("%d",out);}int main(){ build(); solve(); return 0;}

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/8531052.html

你可能感兴趣的文章
北京某公司.NET面试题
查看>>
解决异常“SqlParameterCollection 只接受非空的 SqlParameter 类型对象。”
查看>>
PostgreSQL通过mysql_fdw访问MySQL数据库
查看>>
REST风格的原则
查看>>
Struts分页的一个实现
查看>>
[LintCode] Nuts & Bolts Problem 螺栓螺母问题
查看>>
53.2. group_concat() 列传行
查看>>
I.MX6 SHT20 Linux 驱动移植
查看>>
7.4. String
查看>>
使用PHP配置文件
查看>>
【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现
查看>>
开发网站合集
查看>>
fastcgi配置
查看>>
[转]Java中堆和栈创建对象的区别
查看>>
Android源码浅析(三)——Android AOSP 5.1.1源码的同步sync和编译make,搭建Samba服务器进行更便捷的烧录刷机...
查看>>
咪蒙这么火是怎么做到的
查看>>
【&#9733;】路由环路大总结!
查看>>
Spring源码学习之:ClassLoader学习(5)-自测
查看>>
awesome-nlp
查看>>
第 102 章 Ntop
查看>>