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下gdb调试

    一.gdb常用命令: 命令 描述 backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 info(或 ...

  2. [c++,bson] linux 使用 BSON 编程[www]

    [c++,bson] linux 使用 BSON 编程 http://blog.chinaunix.net/uid-28595538-id-4987410.html 1.js db=db.getSib ...

  3. pycharm模板参数

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

  4. Asp.Net Forms获取UEeditor内容

    UEeditor是比较常用的富文本编辑器. 获取编辑器的内容,需要使用js获取,官方提供的方法是:UE.getEditor('editor').getContent(); 官方提供的.net例子中是使 ...

  5. HDU 5129 Yong Zheng's Death

    题目链接:HDU-5129 题目大意为给一堆字符串,问由任意两个字符串的前缀子串(注意断句)能组成多少种不同的字符串. 思路是先用总方案数减去重复的方案数. 考虑对于一个字符串S,如图,假设S1,S2 ...

  6. [Think] position与anchorPoint关系

    1.简介: 最近在学习动画,所以接触到了CAlayer类. 其中的position与anchorPoint关系一感觉一头雾水,网上有篇文章 彻底理解position和anchorPoint关系  里面 ...

  7. 让我们来一起学习OC吧

    在本分类中的接下来的将翻译http://rypress.com/tutorials/objective-c/index 通过每一章节的翻译,使得自己的OC基础扎实并分享给大家.

  8. C语言面试题总结(一)

    以前的记录都在电子笔记里,倒不如拿出来,有错的地方和大家交流. 1.指针操作: 如下例,设a内存地址为OX00 int a =10; int *p = &a; *a 编译错误 a表示10 *p ...

  9. 用JavaScript校验日期的合法性

    校验表单时可能会遇到校验日期是否正确.可以利用JS的内置对象Date帮助我们完成日期校验. 思路是首先用被校验日期(假设为A,可能为字符串或数字)创建一个Date对象(假设为B). 然后判断A和B的年 ...

  10. linux命令(47):rmdir命令

    1.命令格式: rmdir [选项]... 目录... 2.命令功能: 该命令从一个目录中删除一个或多个子目录项,删除某目录时也必须具有对父目录的写权限. 3.命令参数: - p 递归删除目录dirn ...