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. 关于HashMap中hash()函数的思考

    关于HashMap中hash()函数的思考 JDK7中hash函数的实现   static int hash(int h) { h ^= (h >>> 20) ^ (h >&g ...

  2. Spring-bean(一)

    配置形式:基于xml文件的方式:基于注解的方式 Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 依赖注入的方式:属性注入,构造器注入 ...

  3. JS学习-事件响应小结-简单的计算器

    <!DOCTYPE html> <html> <head> <title> 事件</title> <script type=" ...

  4. 46 Simple Python Exercises-Higher order functions and list comprehensions

    26. Using the higher order function reduce(), write a function max_in_list() that takes a list of nu ...

  5. hasOneOf # if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) {

    if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) { export const hasOneOf = (targetarr, ar ...

  6. 包含绑定变量的sql进行调优需注意一点

    拿1个sql举个例子,我只贴出了where后面部分 实际环境中有init_date 和direct_no的组合索引IDX_DATE_NO 上诉标红处,:b3=0 和:b3<>0这两种情况o ...

  7. Linux-03 Linux下的tar命令

    功能说明 用来建立,还原备份文件的工具程序,它可以加入,解开备份文件内的文件 参数 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五 ...

  8. win手动编译JAVA 未完成(系统path未加入文章)

    java 下面存.BAT dir /s /B *.java > sources.txtjavac @sources.txt -bootclasspath "C:\Users\88797 ...

  9. flash player vista or win7

    win10 or 8 firefox https://fpdownload.adobe.com/get/flashplayer/pdc/31.0.0.122/install_flash_player. ...

  10. Redis进阶例子

    工作中用到的RabbitMQ例子 , 但是最后没有用 , 用的CMQ , 顺便说下CMQ社区真的少 , 并且功能少 . 一.消息体 package com.bootdo.common.rabbitmq ...