【字符串+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 & ...
随机推荐
- VMwareworkstation 12安装
由于这篇博客中有大量截图,一个一个上传太累了,所以请点击以下链接进行查看 百度云:http://pan.baidu.com/s/1bQxPSi 这里直接粘贴文字可以,但是详细的截图贴不出来!
- Suricata的Reputation
见官网 https://suricata.readthedocs.io/en/latest/reputation/index.html Docs » 9. Reputation Edit on Git ...
- windows 7 正确禁用 IPv6
与Windows XP和Windows Server 2003不同的是,Windows Vista和Windows Server 2008中的IPv6无法被卸载.然而,在Windows Vista和W ...
- maven-ali镜像网站setting.xml
简述 使用maven管理jar包的时候,有可能会有网络限制,要解决的这个问题的话可以使用ali的镜像网站. 创建文件 创建settings.xml,内容如下 <settings xmlns=&q ...
- 2018微软实习笔试一道dp题目总结
题意大概是说在一维数轴上起点和终点的距离是d,现在我们要从起点走到终点.每走一个单位长度消耗一个单位能量,初始时有K单位能量.同时在起点和终点之间分布一些加油站a1,a2,...an,给你加油站数量. ...
- js获取服务器生成并返回客户端呈现给客户的控件id的方法
var repeaterId = '<%=rpData.ClientID %>'; //Repeater的客户端IDvar rows = <%=rpData.Items.Count% ...
- (转)Spring4.2.5+Hibernate4.3.11+Struts2.3.24整合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52902851 前面我们已经学会了Spring4.2.5+Hibernate4.3.11+Str ...
- Docker下redis的主从配置
1.拉取redis镜像docker pull redis2.启动3个redis容器服务,分别使用到6379.6380.6381端口docker run --name redis-6379 -p 637 ...
- Python3中的输入输出
input()函数 我们可以通过Python3解释器查看Python3中input()的含义: >>> type(input) <class 'builtin_function ...
- libcmt.lb libcmtd.lib与MSVCRTD.lib的冲突解决
system("pause"); 这个函数存在于MSVCRTD.lib库中: 当要使用system("pause")这个函数,且libcmt.lb libcmt ...