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

题目连接:

http://www.lydsy.com/JudgeOnline/problem.php?id=4032

Description

在虐各种最长公共子串、子序列的题虐的不耐烦了之后,你决定反其道而行之。

一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是。

一个串的“子序列”指的是它的可以不连续的一段,例如bde是abcdef的子串,但bdd不是。

下面,给两个小写字母串A,B,请你计算:

(1) A的一个最短的子串,它不是B的子串

(2) A的一个最短的子串,它不是B的子序列

(3) A的一个最短的子序列,它不是B的子串

(4) A的一个最短的子序列,它不是B的子序列

Input

有两行,每行一个小写字母组成的字符串,分别代表A和B。

Output

输出4行,每行一个整数,表示以上4个问题的答案的长度。如果没有符合要求的答案,输出-1.

Sample Input

aabbcc

abcabc

Sample Output

2

4

2

4

Hint

对于100%的数据,A和B的长度都不超过2000

题意

题解:

bfs四合一

答案都是最长公共子XX+1

子串我们用后缀自动机去跑,子序列我们用dp的贪心去跑就好了

那个dp表示从i位置走到字符j的最小位置

注意后缀自动机要开两倍空间……因为这个,wa成傻逼了

代码

#include<bits/stdc++.h>
using namespace std; const int maxn = 2e3+5;
char s1[maxn],s2[maxn];
int len1,len2;
int go1[maxn][26],go2[maxn][26];
int vis[maxn][maxn*2];
int Son[maxn*2][26],pre[maxn*2],len[maxn*2],la=1,Now=1;
struct SAM
{
void add(int x)
{
int p=la,np=la=++Now;
len[np]=len[p]+1;
for(;p&&!Son[p][x];p=pre[p])
Son[p][x]=np;
if(!p)
pre[np]=1;
else
{
int q=Son[p][x];
if(len[q]==len[p]+1)
pre[np]=q;
else
{
int nq=++Now;
memcpy(Son[nq],Son[q],sizeof Son[nq]);
len[nq]=len[p]+1;
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
for(;p&&Son[p][x]==q;p=pre[p])
Son[p][x]=nq;
}
}
}
}sam; struct node
{
int x,y,l;
};
queue<node>Q;
void init()
{
memset(vis,0,sizeof(vis));
while(!Q.empty())Q.pop();
}
int solve1()
{
init();
for(int i=0;i<len1;i++)
Q.push(node{i,1,0});
while(!Q.empty())
{
node Now = Q.front();
Q.pop();
if(Now.x==len1)continue;
int nextx=Now.x+1,nexty=Son[Now.y][s1[nextx]-'a'];
if(nexty==0)return Now.l+1;
if(!vis[nextx][nexty])
vis[nextx][nexty]=1,Q.push(node{nextx,nexty,Now.l+1});
}
return -1;
}
int solve2()
{
init();
for(int i=0;i<len1;i++)
Q.push(node{i,0,0});
while(!Q.empty())
{
node Now = Q.front();
Q.pop();
if(Now.x==len1)continue;
int nextx=Now.x+1,nexty=go2[Now.y][s1[Now.x+1]-'a'];
if(nexty==1e5)return Now.l+1;
if(!vis[nextx][nexty])
vis[nextx][nexty]=1,Q.push(node{nextx,nexty,Now.l+1});
}
return -1;
}
int solve3()
{
init();
Q.push(node{0,1,0});
while(!Q.empty())
{
node Now = Q.front();
Q.pop();
for(int i=0;i<26;i++)
{
int nextx=go1[Now.x][i],nexty=Son[Now.y][s1[nextx]-'a'];
if(nextx==1e5)continue;
if(nexty==0)return Now.l+1;
if(!vis[nextx][nexty])
vis[nextx][nexty]=1,Q.push(node{nextx,nexty,Now.l+1});
}
}
return -1;
}
int solve4()
{
init();
Q.push(node{0,0,0});
while(!Q.empty())
{
node Now = Q.front();
Q.pop();
for(int i=0;i<26;i++)
{
int nextx=go1[Now.x][i],nexty=go2[Now.y][i];
if(nextx==1e5)continue;
if(nexty==1e5)return Now.l+1;
if(!vis[nextx][nexty])
vis[nextx][nexty]=1,Q.push(node{nextx,nexty,Now.l+1});
}
}
return -1;
}
int main()
{
scanf("%s%s",s1+1,s2+1);
len1=strlen(s1+1),len2=strlen(s2+1);
for(int i=1;i<=len2;i++)sam.add(s2[i]-'a');
for(int i=0;i<maxn;i++)for(int j=0;j<26;j++)
go1[i][j]=go2[i][j]=1e5;
for(int i=0;i<=len1;i++)
for(int j=i+1;j<=len1;j++)
go1[i][s1[j]-'a']=min(go1[i][s1[j]-'a'],j);
for(int i=0;i<=len2;i++)
for(int j=i+1;j<=len2;j++)
go2[i][s2[j]-'a']=min(go2[i][s2[j]-'a'],j);
printf("%d\n%d\n%d\n%d\n",solve1(),solve2(),solve3(),solve4());
}

BZOJ 4032: [HEOI2015]最短不公共子串 后缀自动机 暴力的更多相关文章

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

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

  2. BZOJ 4032: [HEOI2015]最短不公共子串(后缀自动机+记忆化搜索)

    传送门 解题思路 首先需要预处理两个串\(nxt(i)(j)\)表示i位置之后最近的\(j\). 第一问直接对\(b\)建后缀自动机,枚举\(a\)的起点暴力匹配. 第二问枚举\(a\)的起点,\(b ...

  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. linux下终端游戏

    sl 一列火车 oneko 一只淘气的小猫

  2. pycharm模板参数

    # -*- coding: utf-8 -*-# @Time : ${DATE} ${TIME}# @Author : cxa# @File : ${NAME}.py# @Software: ${PR ...

  3. APP运营

    产品相关术语 APP:application的简写,即应用. 开发商:也叫CP,即ContentProvider内容提供商. 发行商(运营商):代理CP开发出来的产品. 联运:CP和渠道联合运营产品. ...

  4. 欢迎访问新博客aiyoupass.com

    新博客基本搭建好了,欢迎访问.aiyoupass.com

  5. P2885

    2885 code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; b ...

  6. 阿里云ftp连接遇到的错误,entering passive mode失败(一个并不成熟的产品?)

    ####禁止自行转载 ####违者必究 ####需要转载请联系我 因为最近用python写了一个自动上传的脚本,想通过FTP把数据上传到我的服务器. 于是, 兴高采烈地打开我一直信任的阿里云,yum ...

  7. python 基础习题

    1.8<<2等于? 8 ---> 1000 32 ---> 100000 -----------结果--- 32 2.通过内置函数计算5除以2的余数 print(dir()) ...

  8. 论文笔记 《Maxout Networks》 && 《Network In Network》

    论文笔记 <Maxout Networks> && <Network In Network> 发表于 2014-09-22   |   1条评论 出处 maxo ...

  9. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  10. 走进 UnitTest for Xamarin.Forms

    之前讲了 Xamarin.Forms 的 UITest 走进 UITest for Xamarin.Forms 走进 Xamarin Test Recorder for Xamarin.Forms 但 ...