【字符串+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 & ...
随机推荐
- Apache Cordova
http://cordova.apache.org/ Apache Cordova is a platformfor building native mobile applications using ...
- Python 版本对比
python2 与 python3可认为代码不通用,你也可以点击Python2.x与3.x版本区别来查看两者的不同 python3.6以上支持f-string,一种很方便的变量替换方式 高版本可能 ...
- UVa OJ 458
The Decoder Write a complete program that will correctly decode a set of characters into a valid m ...
- Can't locate ExtUtils/MakeMaker.pm in @INC
Can't locate ExtUtils/MakeMaker.pm in @INC 解决办法:yum install perl-devel
- 《深入理解Java虚拟机》读书笔记
堆分配参数: -XX:+PrintGC 使用该参数,虚拟机启动后,只要遇到GC就会打印日志: -XX:+UseSerialGC 配置串行回收器: -XX:+PrintGCDeltails 可以查看详细 ...
- 项目中常用git命令操作指令(一般正常的话够用不够再看相关git命令)
配置git1.首先在本地创建ssh key:ssh-keygen -t rsa -C "github上注册的邮箱" //(一路回车)2.进入c:/Users/xxxx_000/.s ...
- HDU 5380 Travel with candy (贪心,单调队列)
题意: 有n+1个城市按顺序分布在同一直线上,现在需从0号城市按顺序走到n号城市(保证可达),从0号城市到i号城市需要消耗ai个糖果,每个城市都可以通过买/卖糖果来赚取更多的钱,价格分别是buyi和s ...
- Jenkins执行sudo权限的设置
Jenkins系统中添加执行脚本的时候,有一些命令是需要sudo权限和来执行的,可以在root权限下添加一下Jenkins账号的权限 1.添加不需要密码可sudo执行指定命令的权限 cd /etc c ...
- Linux OpenGL 实践篇-10-framebuffer
在之前的实践中我们都是在当前的窗口中渲染,即使用的缓存都是由glutCreateWindow时创建的缓存,我们可称之为默认缓存.它是唯一一个可以被图形服务器的显示系统识别的帧缓存,我们在屏幕上看到的只 ...
- SQLite – LIMIT子句
SQLite - LIMIT子句 SQLite LIMIT子句是用来限制SELECT语句返回的数据量. 语法: SELECT语句.LIMIT子句的基本语法如下: SELECT column1, col ...