【字符串+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 & ...
随机推荐
- RedHat改yum源免费使用CentOS源
linux默认是安装了yum软件的,但是由于激活认证的原因让redhat无法直接进行yum安装一些软件 如果我们需要在redhat下直接yum安装软件,我们只用把yum的源修改成CentOS的就好了, ...
- Java提供的序列化和反序列化
序列化:是指将Java对象转换为二进制数据. 反序列化:将二进制数据转换为Java对象. 与序列化功能相关的类有: java.io.Serializable; java.io.ObjectOutput ...
- T4312 最大出栈顺序
题目描述 给你一个栈和n个数,按照n个数的顺序入栈,你可以选择在任何时候将数 出栈,使得出栈的序列的字典序最大. 输入输出格式 输入格式: 输入共2行. 第一行个整数n,表示入栈序列长度. 第二行包含 ...
- 小程序canvas截图组件
最近做一个小程序的过程中,需要用到截图功能,网上搜了一下,发现没有符合要求的,就自己搞了个组件,方便复用. 目前功能很简单,传入宽高和图片路径即可,宽高是为了计算截图的比例,只支持缩放和移动. 实现思 ...
- java操作Excel、PDF文件
java操作Excel.PDF文件 分享者:Vashon 分享来源:CSDN博客 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的 ...
- php用面向对象从mysql取数据
<?php //建立数据库的链接@$_mysqli = new mysqli('localhost','root','123456','dbname');if(mysqli_connect_er ...
- 洛谷 P2319 [HNOI2006]超级英雄
题目描述 题目描述 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回答一 ...
- (转)Spring提供的CharacterEncoding和OpenSessionInView功能
http://blog.csdn.net/yerenyuan_pku/article/details/52902282 前面我们以一种更加优雅的方式集成了Spring4.2.5+Hibernate4. ...
- Java并发编程之原子操作类
什么是原子操作类当更新一个变量的时候,多出现数据争用的时候可能出现所意想不到的情况.这时的一般策略是使用synchronized解决,因为synchronized能够保证多个线程不会同时更新该变量.然 ...
- 【整理】iview中刷新页面的时候更新导航菜单的active-name
iview中刷新页面的时候更新导航菜单的active-name https://blog.csdn.net/lhjuejiang/article/details/83212070