BZOJ 4032: [HEOI2015]最短不公共子串 后缀自动机 暴力
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]最短不公共子串 后缀自动机 暴力的更多相关文章
- bzoj 4032 [HEOI2015]最短不公共子串——后缀自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4032 不是 b 的子串的话就对 b 建后缀自动机,在 a 上枚举从每个位置开始的子串或者找子 ...
- BZOJ 4032: [HEOI2015]最短不公共子串(后缀自动机+记忆化搜索)
传送门 解题思路 首先需要预处理两个串\(nxt(i)(j)\)表示i位置之后最近的\(j\). 第一问直接对\(b\)建后缀自动机,枚举\(a\)的起点暴力匹配. 第二问枚举\(a\)的起点,\(b ...
- bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)
bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...
- BZOJ 4032: [HEOI2015]最短不公共子串
4032: [HEOI2015]最短不公共子串 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 446 Solved: 224[Submit][Sta ...
- BZOJ.4032.[HEOI2015]最短不公共子串(DP 后缀自动机)
题目链接 1.求A的最短子串,它不是B的子串. 子串是连续的,对B建SAM,枚举起点,在SAM上找到第一个无法匹配点即可.O(n)用SAM能做吗..开始想错了. 2.求A的最短子串,它不是B的子序列. ...
- 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] ...
- BZOJ 4032: [HEOI2015]最短不公共子串 (dp*3 + SAM)
转博客大法好 第4个子任务中,为什么只转移最近的一个位置,自己YY吧(多YY有益身体健康). #include <bits/stdc++.h> using namespace std; t ...
- bzoj 4032 [ HEOI 2015 ] 最短不公共子串 —— 后缀自动机+序列自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4032 序列自动机其实就是每个位置记录一下某字母后面第一个出现位置,为了子序列能尽量长. 对字 ...
- 【BZOJ】4032: [HEOI2015]最短不公共子串(LibreOJ #2123)
[题意]给两个小写字母串A,B,请你计算: (1) A的一个最短的子串,它不是B的子串 (2) A的一个最短的子串,它不是B的子序列 (3) A的一个最短的子序列,它不是B的子串 (4) A的一个最短 ...
随机推荐
- 基于Android的简单聊天工具-服务器端
1.数据库用的mysql,一共有3张表,一张用户表user.一张朋友列表friend和一张消息表message. 1 User table 用户表 uid 主键自动生成 userName 昵称 use ...
- 为什么IO多路复用需要采用非阻塞式IO
近段时间开始学习<Unix网络编程>,代码实现了一个简单的IO多路复用+阻塞式的服务端,在学习了非阻塞式IO后,有一个疑问,即: 假如调用了select,并且关注了几个描述字,当关注的描述 ...
- python实战===百度文字识别sdk
http://ai.baidu.com/docs#/OCR-Python-SDK/top
- C# 对后台方法事件,可以直接return; 跳出
protected void lbtButton_Click(object sender, EventArgs e) { return; } C# 对后台方法事件,可以直接return; 跳出
- BZOJ 4516: [Sdoi2016]生成魔咒——后缀数组、并查集
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4516 题意 一开始串为空,每次往串后面加一个字符,求本质不同的子串的个数,可以离线.即长度为 ...
- 基于rest_framework和redis实现购物车的操作,结算,支付
前奏: 首先,要在主机中安装redis,windows中安装,下载一个镜像,直接进行下一步的安装,安装成功后,在cmd中输入redis-cli 安装python的依赖库: redis 和 ...
- hdu 2389(二分图hk算法模板)
Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Ot ...
- Linked List Cycle I&&II——快慢指针(II还没有完全理解)
Linked List Cycle I Given a linked list, determine if it has a cycle in it. Follow up: Can you solve ...
- 【转载】python 特殊函数 dunder function
python的特殊方法:另外一种称谓是 dunder function, 就是 under-under function的简写,就是指那些前后都带双下划线的函数. 转自这里: https://blog ...
- SEO编辑必看:撰写搜索引擎喜爱的标题
导读:非常有干货,百度站长平台刚发布了这篇篇文章,文章建议:1,标题字数控制在65个字节内,2,重要内容放在标题的最前面,3,添加与网页内容最相关的.用户更常用的.满足用户明确需求的.体现时效性.关键 ...