【字符串+BFS】Problem 7. James Bond
https://www.bnuoj.com/v3/external/gym/101241.pdf
【题意】
- 给定n个字符串,大小写敏感
- 定义一个操作:选择任意m个串首尾相连组成一个新串
- 问是否存在一个这样的串s,s可以由不同的串首尾相连得到
- 最多100个字符串,所有字符串的总长度不超过5000
【样例解释】

aB5可以由a+B5得到,也可以由aB+5得到,所以输出YES
【思路】
- 首先一定是在100个选择2个串a,b,a是b的前缀
- 然后a和b的前缀可以消去,我们想知道b剩下的右半部分是哪一个串的前缀
- 找到这个串后前缀也可以消去,然后再找剩下的部分
- 因为数据范围不是很大,所以可以暴力搜索,bfs 700ms过
- vis[i][j]在bfs中去重,代表是i字符串的以j开始的后缀
【AC】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue> using namespace std;
int n;
const int maxn=5e4+;
char str[][maxn];
int len[];
int flag;
bool vis[][maxn];
struct node
{
int id;
int len;
node(){}
node(int _id,int _len):id(_id),len(_len){}
};
bool solve()
{
queue<node> Q;
for(int i=;i<n;i++)
{
for(int j=;j<i;j++)
{
int l=min(len[i],len[j]);
int tot=;
for(int k=;k<l;k++)
{
if(str[i][k]==str[j][k]) tot++;
else break;
}
if(tot!=l) continue;
if(len[i]==len[j]) return true;
if(len[i]<len[j])
{
vis[j][len[i]]=true;
Q.push(node(j,len[i]));
}
else
{
vis[i][len[j]]=true;
Q.push(node(i,len[j]));
}
}
}
while(!Q.empty())
{
node q=Q.front(); Q.pop();
for(int i=;i<n;i++)
{
int lq=len[q.id]-q.len;
int l=min(lq,len[i]);
int tot=;
for(int j=;j<l;j++)
{
if(str[i][j]==str[q.id][q.len+j]) tot++;
else break;
if(tot==l)
{
if(lq==len[i]) return true;
else if(lq<len[i])
{
if(!vis[i][lq])
{
vis[i][lq]=true;
Q.push(node(i,lq));
}
}
else
{
if(!vis[q.id][q.len+len[i]])
{
vis[q.id][q.len+len[i]]=true;
Q.push(node(q.id,q.len+len[i]));
}
} }
}
}
}
return false;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",str[i]);
len[i]=strlen(str[i]);
}
if(solve())
{
puts("YES");
}
else
{
puts("NO");
}
return ;
}
【字符串+BFS】Problem 7. James Bond的更多相关文章
- PTA 07-图5 Saving James Bond - Hard Version (30分)
07-图5 Saving James Bond - Hard Version (30分) This time let us consider the situation in the movie ...
- Saving James Bond(dijk)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1245 Saving James Bond Time Limit: 6000/3000 MS (Java ...
- 07-图5 Saving James Bond - Hard Version (30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 07-图5 Saving James Bond - Hard Version (30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 07-图5 Saving James Bond - Hard Version(30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version
题目来源:http://pta.patest.cn/pta/test/18/exam/4/question/625 This time let us consider the situation in ...
- PTA 06-图2 Saving James Bond - Easy Version (25分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version (25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
随机推荐
- (办公)定时任务quartz入门
1.简单入门. 2.定时任务注入service. 入门案例: 1.1. 加jar <dependency> <groupId>org.quartz-scheduler</ ...
- vmware让虚拟机内外网络可互访
以下方法可使主机可以ping通虚拟机,虚拟机也可以ping通主机 首先对虚拟机设置 然后设置虚拟机,假设主机的ip是10.0.0.9,那虚拟机的ip应如下设置: 其中ip地址任意设置一个,但要求跟主机 ...
- pspad的一个怪现象:在一些空行的位置出现个别不该出现的字符
在用pspad编辑一个外来文件时,发现有许多空行的结尾会出现一些单个字符,字符内容与翻页前那一页相应位置的字符相同. 好奇怪.上网找不到原因.pspad太好用了,不想因此放弃. 仔细观察,这些空行往往 ...
- UVA 11922 Permutation Transformer (Splay树)
题意: 给一个序列,是从1~n共n个的自然数,接下来又m个区间,对于每个区间[a,b],从第a个到第b个从序列中分离出来,翻转后接到尾部.输出最后的序列. 思路: 这次添加了Split和Merge两个 ...
- 迅为4412开发平台Zigbee模块在物联网智能家居中的应用
物联网智能家居的发展物联网随着互联网的发展,可以通过互联网实现物和物的互联,就有了物联网的概念.传统家居电器 有了物联网之后,在家居电器范围中,就是通过物联网技术将家中的各种设备连接到一起,家居中 ...
- 几个有关整数的证明(from信息安全数学基础的作业)
1. 设m,n为正整数,m为奇数,求证2m-1和2n+1互素 反证法:假设d=(2m-1,2n+1)≥2,则存在x,y∈z,2m=dx+1,和2n=dy-1 则存在u,v∈z,2mn=du+1,2nm ...
- upload 上传按钮组件 iview
<!-- * @description 导入Excel * @fileName importExcel.vue * @author 彭成刚 * @date // :: * @version V1 ...
- Spring自动注入的几种方式
---恢复内容开始--- @Service("accountEmailService")public class AccountEmailServiceImpl impleme ...
- 把特征网络换成resnet-50
从RFCN来看,Resnet-50和Resnet-101到最后一层卷积都是缩小到原来尺寸的16分之一,并且都用的7x7的格子去roi pooling. 看paper可以知道:resnet-50核心是由 ...
- CPP-STL:list容器
本文以List容器为例子,介绍了STL的基本内容,从容器到迭代器,再到普通函数,而且例子丰富,通俗易懂.不失为STL的入门文章,新手不容错过! 目录 1 定义一个list 2 使用list的成员函数p ...