传送门

解题思路

  首先需要预处理两个串$nxt(i)(j)$表示i位置之后最近的$j$。
  第一问直接对$b$建后缀自动机,枚举$a$的起点暴力匹配。
  第二问枚举$a$的起点,$b$用$nxt$跳。
  第三问$a$与$b$一起跳,$b$用后缀自动机,$a$用$nxt$。
  第四问$a$与$b$一起跳,都用$nxt$,要加记忆化。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std;
const int N=2005; inline int min(int x,int y) {return x<y?x:y;} int lena,lenb,ans=1e9,nxta[N][27],prea[27],nxtb[N][27],preb[27];
int vis[N][N];
char A[N],B[N]; struct SAM{
int lst,cnt,fa[N<<1],ch[N<<1][27],l[N<<1];
inline void insert(int c){
int p=lst,np=++cnt; lst=cnt; l[np]=l[p]+1;
for(;p && !ch[p][c];p=fa[p]) ch[p][c]=np;
if(!p) fa[np]=1;
else {
int q=ch[p][c];
if(l[q]==l[p]+1) fa[np]=q;
else {
int nq=++cnt; l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
}
}
}
inline int query1(int x){
int p=1,ret=0;
for(int i=x;i<=lena;i++) {
if(ch[p][A[i]-'a']) p=ch[p][A[i]-'a'],ret++;
else return ret+1;
}
return 1e9;
}
inline void query3(int x,int y,int l){
for(int i=0;i<26;i++){
if(!ch[y][i] && nxta[x][i]) ans=min(ans,l);
else if(ch[y][i] && nxta[x][i]) query3(nxta[x][i],ch[y][i],l+1);
}
}
}sam; inline int query2(int x){
int now=0,ret=0;
for(int i=x;i<=lena;i++){
if(!nxtb[now][A[i]-'a']) return ret+1;
ret++; now=nxtb[now][A[i]-'a'];
}
return 1e9;
} int query4(int x,int y){
if(x && !y) return 1;
if(vis[x][y]) return vis[x][y];
int tmp=1e9;
for(int i=0;i<26;i++){
if(!nxta[x][i]) continue;
tmp=min(tmp,query4(nxta[x][i],nxtb[y][i]));
}
vis[x][y]=tmp+1;
return vis[x][y];
} int main(){
scanf("%s%s",A+1,B+1); sam.cnt=sam.lst=1;
lena=strlen(A+1); lenb=strlen(B+1);
for(int i=lena;~i;i--) {
for(int j=0;j<26;j++) nxta[i][j]=prea[j];
prea[A[i]-'a']=i;
}
for(int i=lenb;~i;i--){
for(int j=0;j<26;j++) nxtb[i][j]=preb[j];
preb[B[i]-'a']=i;
}
for(int i=1;i<=lenb;i++) sam.insert(B[i]-'a');
for(int i=1;i<=lena;i++) ans=min(ans,sam.query1(i));
printf("%d\n",(ans==1e9)?-1:ans); ans=1e9;
for(int i=1;i<=lena;i++) ans=min(ans,query2(i));
printf("%d\n",(ans==1e9)?-1:ans); ans=1e9; sam.query3(0,1,0);
printf("%d\n",(ans==1e9)?-1:ans+1); ans=query4(0,0)-1;
printf("%d\n",(ans==1e9)?-1:ans);
return 0;
}

BZOJ 4032: [HEOI2015]最短不公共子串(后缀自动机+记忆化搜索)的更多相关文章

  1. BZOJ 4032: [HEOI2015]最短不公共子串 后缀自动机 暴力

    4032: [HEOI2015]最短不公共子串 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4032 Description 在虐各种最 ...

  2. bzoj 4032 [HEOI2015]最短不公共子串——后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4032 不是 b 的子串的话就对 b 建后缀自动机,在 a 上枚举从每个位置开始的子串或者找子 ...

  3. bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)

    bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...

  4. BZOJ 4032: [HEOI2015]最短不公共子串

    4032: [HEOI2015]最短不公共子串 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 446  Solved: 224[Submit][Sta ...

  5. BZOJ.4032.[HEOI2015]最短不公共子串(DP 后缀自动机)

    题目链接 1.求A的最短子串,它不是B的子串. 子串是连续的,对B建SAM,枚举起点,在SAM上找到第一个无法匹配点即可.O(n)用SAM能做吗..开始想错了. 2.求A的最短子串,它不是B的子序列. ...

  6. bzoj 4032: [HEOI2015]最短不公共子串【dp+SAM】

    第一.二问: 就是最小的最长公共长度+1,设f[i][j]为a匹配到i,b匹配到j,第一问的转移是f[i][j]=(a[i]==b[j]?f[i-1][j-1]+1:0),第二问的转移是f[i][j] ...

  7. BZOJ 4032: [HEOI2015]最短不公共子串 (dp*3 + SAM)

    转博客大法好 第4个子任务中,为什么只转移最近的一个位置,自己YY吧(多YY有益身体健康). #include <bits/stdc++.h> using namespace std; t ...

  8. bzoj 4032 [ HEOI 2015 ] 最短不公共子串 —— 后缀自动机+序列自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4032 序列自动机其实就是每个位置记录一下某字母后面第一个出现位置,为了子序列能尽量长. 对字 ...

  9. 【BZOJ】4032: [HEOI2015]最短不公共子串(LibreOJ #2123)

    [题意]给两个小写字母串A,B,请你计算: (1) A的一个最短的子串,它不是B的子串 (2) A的一个最短的子串,它不是B的子序列 (3) A的一个最短的子序列,它不是B的子串 (4) A的一个最短 ...

随机推荐

  1. CSS中的一些伪类

    一.:nth-child 和 :nth-of-type (1):nth-child() :nth-child(n) 选择器选取某任意一父元素的第 n 个子元素( p:nth-child(n) 即选中任 ...

  2. 转载:解决npm安装时出现run `npm audit fix` to fix them, or `npm audit` for details

    转载自:https://blog.csdn.net/qq_39165556/article/details/89333028 1.第一种解决办法 npm audit fix npm audit fix ...

  3. ROM、RAM、DRAM、SRAM、FLASH的区别?

    在学习单片机的时候经常会被这些东西搞晕掉,什么ROM RAM FLASH EEPROM 等等......为了不被搞晕,做个笔记,不记得的时候过来看看. 下面是我在网上找的资料: ROM和RAM指的都是 ...

  4. 【python】 字符串转小写(含汉字等时仍work)

    def mylower(str): outstr = ""; strlen = len(str); idx = 0; while idx < strlen: if ord(s ...

  5. vue 中 element-ui 引入方式

    目录 前言 全部引用 单个引用 前言 有时候只会使用到 Element-ui 的部分功能,为了减少文件体积建议使用分开引用,即只引用使用的功能. 注意:在main.js中使用部分引用的时候是 impo ...

  6. Eclipse插件pydev编辑.py文件时报错:unresolved import error.解决办法

    在同一个包中import还报unresolved import error.感觉很奇怪,原来需要把当前的包也要添加到System libs中  

  7. jumpserver4.0centos7安装步骤

    一步一步安装(CentOS) 测试推荐环境 CPU: 64位双核处理器 内存: 4G DDR3 数据库:mysql 版本大于等于 5.6 mariadb 版本大于等于 5.5.6 环境 系统: Cen ...

  8. SAP Material Type on Classification Tree(ClassMaster management)

    SAP Material Type on Classification Tree(ClassMaster management) 1. Classification Tree 是 luxottica ...

  9. kubernetes容器集群管理部署node节点组件

    发送配置文件到各个节点 [root@master ~]# scp /opt/kubernetes/cfg/*kubeconfig root@192.168.238.128:/opt/kubernete ...

  10. HTTP Request 422 Unprocessable Entity

    最近接了一个接口,在调用接口时,返回这个错误代码. 百度到的解释是:请求格式正确,但是由于含有语义错误,无法响应. 开始一直在纠结,语义错误到底是什么?对照了无数次参数名,传参方式,无解. 后来用Fi ...