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. Python自动化运维 - Django(三)CSRF - Cookie&Session

    CSRF跨站请求伪造 CSRF跨站点请求伪造(Cross—Site Request Forgery),跟XSS攻击一样,存在巨大的危害性,你可以这样来理解:攻击者盗用了你的身份,以你的名义发送恶意请求 ...

  2. 日常开发技巧:使用notify-send发送通知

    背景 在终端执行一些需要较长时间的命令时,会切换到别的界面.但为了知道是否执行完成,需要时不时地切换过去看一眼.很麻烦. 解决方式 为了减少这种麻烦,可以使用notify-send,发送桌面通知.no ...

  3. SD 模拟sip 读写子程序

    void simulate_spi_write_byte(u8 data){ u8 kk; SPI3_CS(0); SPI3_SCK(0); delay_us(1); //???spi???1/2us ...

  4. 安全测试===sqlmap

    本文转自:https://www.secpulse.com/archives/4213.html   鉴于很多新手对sqlmap的用法不是很熟悉 很多常用sqlmap的也不一定完全会用sqlmap 特 ...

  5. 网络知识===wireshark抓包,三次握手分析

    TCP需要三次握手建立连接: 网上的三次握手讲解的太复杂抽象,尝试着使用wireshark抓包分析,得到如下数据: 整个过程分析如下: step1 client给server发送:[SYN] Seq ...

  6. python的时间和日期--time、datetime应用

    time >>> import time >>> time.localtime() #以time.struct_time类型,打印本地时间 time.struct_ ...

  7. JS中Unix时间戳转换日期格式

    <!doctype html> <html> <head> <title>Unix时间戳转换成日期格式</title> <script ...

  8. 【bzoj1923】外星千足虫

    这个gauss消元有点naive啊. 由于只有01,位操作显然是方便的多. 那么用bitset代替之前的增广矩阵就行了. #include<bits/stdc++.h> #define N ...

  9. JDK1.8特性实现jdk动态代理,使业务解耦

    首先,先创建一个interface IHello 目标接口类 interface IHello { void sayHello(); } 然后再写一个目标类的实现类 class HelloImpl i ...

  10. 三:ZooKeeper的ZAB协议

    一:ZAB协议概述--->ZooKeeper并没有完全采用Paxos算法,而是使用了一种称为ZooKeeper Atomic Broadcast(ZAB,zookeeper原子消息广播协议)的协 ...