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的更多相关文章

  1. 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 ...

  2. Saving James Bond(dijk)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1245 Saving James Bond Time Limit: 6000/3000 MS (Java ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

随机推荐

  1. mongoDB内置文档定义

    在最近的设计数据库时,犯了一个低级的错误,就是设置内置文档是定义了错误了,导致数据取不出,去找了很多资料都无法解决.最后看了一了一下自己设置的model文件.配置错误,所以导致数据取不出了. 数据库时 ...

  2. poj3616 Milking Time

    思路: dp. 实现: #include <iostream> #include <cstdio> #include <algorithm> using names ...

  3. hihocoder1133 二分·二分查找之k小数

    思路: 类似于快排的分治算法. 实现: #include <iostream> #include <cstdio> #include <algorithm> #in ...

  4. BZOJ3083: 遥远的国度(树链剖分)

    题意 $n$个节点的树,每个点有权值,支持三种操作 1. 换根 2.把$x$到$y$路径上节点权值变为$z$ 3.询问路径最小值 Sol 啥?你说这是TopTree的裸题?那你写去啊 很显然,如果没有 ...

  5. 如何破解密码的哈希值,破解双MD5密码值

    这是关于我如何破解密码的哈希值1亿2200万* John the Ripper和oclHashcat-plus故事. 这是几个月前,当我看到一条推特:从korelogic约含共1亿4600万个密码的密 ...

  6. SQLite -分离数据库

    SQLite -分离数据库 SQLite分离DTABASE语句用于分离和分离命名数据库从一个数据库连接之前附加使用附加语句.如果相同的数据库文件已附加多个别名,然后分离命令将断开只有名字和其他依附仍将 ...

  7. 产生多种anchor的代码讲解!很好!

    http://blog.csdn.net/xzzppp/article/details/52317863 源代码:https://github.com/rbgirshick/py-faster-rcn ...

  8. MySQL-06 数据备份和恢复

    学习目标 数据备份 数据恢复 数据库迁移 导入和导出 数据备份 系统意外崩溃或者服务器硬件损坏都有可能导致数据库丢失,因此生产环境中数据备份非常重要. MySQLdump命令备份 该命令可以将数据库备 ...

  9. MySQL丨03丨基本语句

    MySQL语句都是以 ; 号结尾的 看库(刘大婶直接面对的是各种档案袋) show databases; 建库(新弄了一个档案袋) create database database_name; 删库( ...

  10. python虚拟环境的搭建及作用

    Python的虚拟环境可以使一个Python程序拥有独立的库library和解释器interpreter,而不用与其他Python程序共享统一个library和interpreter.虚拟环境的好处是 ...