F. Fixing Banners
time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

Harbin, whose name was originally a Manchu word meaning "a place for drying fishing nets", grew from a small rural settlement on the Songhua River to become one of the largest cities in Northeast China. Founded in 1898 with the coming of the Chinese Eastern Railway, the city first prospered as a region inhabited by an overwhelming majority of the immigrants from the Russian Empire. Now, Harbin is the capital of Heilongjiang province and the largest city in the northeastern region of the People's Republic of China. It serves as a key political, economic, scientific, cultural, and communications hub in Northeast China, as well as an important industrial base of the nation.

This year, a CCPC regional contest is going to be held in this wonderful city, hosted by Northeast Forestry University. To ensure the contest will be a success and enjoyed by programmers around the country, preparations for the event are well underway months before the contest.

You are the leader of a student volunteer group in charge of making banners to decorate the campus during the event. Unfortunately, your group made a mistake and misprinted one of the banners. To be precise, the word "harbin" is missing in that banner. Because you don't have time to reprint it, the only way to fix it is to cut letters from some used old banners and paste them onto the misprinted banner. You have exactly six banners, and for some reason, you must cut exactly one letter from each banner. Then, you can arrange and paste the six letters onto the misprinted banner and try to make the missing word "harbin". However, before you start cutting, you decide to write a program to see if this is possible at all.

Input

The input contains multiple cases. The first line of the input contains a single integer T (1≤T≤50000)T (1≤T≤50000), the number of cases.

For each case, the input contains six lines. Each line contains a non-empty string consisting only of lowercase English letters, describing the letters on one of the old banners.

The total length of all strings in all cases doesn't exceed 2⋅1062⋅106.

Output

For each case, print the string "Yes" (without quotes) if it is possible to make the word "harbin", otherwise print the string "No" (without quotes).

Example
input

Copy
2
welcome
toparticipate
inthe
ccpccontest
inharbin
inoctober
harvest
belong
ninja
reset
amazing
intriguing
output

Copy
No
Yes
       签到题。不过自己搞复杂了,明明可以用一个技巧,但是我加了6个for,TLE了。
  这个题是输入六条字符串,每一条仅只能截取一个字符,看能否组成harbin。
  我想的算法是,把这个东西转成一个二维矩阵来进行dfs求解。(队友暴力过了,代码极其暴力,惨无人道..我表示看不懂....)
    j 1  2  3  4  5  6
     h  a  r  b  i  n
i  1
  2
  3
   4
  5
  6
  把输入的东西转化为01矩阵,i对应6条字符串,比如样例第一条harvest,出现了h,a,r,那么在i=1行处,横着分别为  1  1  1  0  0  0
  这里有个技巧
    id['h']=;
id['a']=;
id['r']=;
id['b']=;id['i']=;id['n']=;

    这样的话,一个字母对应一个值,比如输入字符串a,   e[ i ] [ id[ a[j] ] ] 就可以了。

    录入过程就是下面这个亚子:

        for(int i=;i<=;i++)
{
scanf("%s",s);
vis[i]=false;
int len=strlen(s);
for(int j=;j<len;j++)
{
e[i][id[s[j]]]=;
}
}

    那么接下来就是DFS了

void dfs(int idx)
{
if(ok)
return ;
if(idx==)
{
ok=;return ;
}
for(int i=;i<=;i++)
{
if(!vis[i]&&e[i][idx])
{
vis[i]=true;
dfs(idx+);
vis[i]=false;
}
}
}

  思想是:idx表示字母序号,由于需要完整的harbin,所以从  idx=1  开始;idx进去以后,for里的i表示第几条字符串,如果有这么一条字符串,未被使用而且存在idx这个字符,那么记录在案,标记已使用,dfs(idx+1)找下一个字母。如果idx==7了,肯定OK,终止输出YES,否则是NO

  最后,每次记得初始化
  
#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn=2e6+;
using namespace std;
char s[maxn];
int e[][];
bool vis[];
int id[];
int ok=;
void dfs(int idx)
{
if(ok)
return ;
if(idx==)
{
ok=;return ;
}
for(int i=;i<=;i++)
{
if(!vis[i]&&e[i][idx])
{
vis[i]=true;
dfs(idx+);
vis[i]=false;
}
}
}
int main()
{
int t;
scanf("%d",&t);
id['h']=;
id['a']=;
id['r']=;
id['b']=;id['i']=;id['n']=;
while(t--)
{
memset(e,,sizeof(e));
ok=;
for(int i=;i<=;i++)
{
scanf("%s",s);
vis[i]=false;
int len=strlen(s);
for(int j=;j<len;j++)
{
e[i][id[s[j]]]=;
}
}
dfs();
if(ok)
printf("Yes\n");
else
printf("No\n");
}
return ;
}

另一个录入方法:

  

for(int i=;i<=;i++)
{
scanf("%s",a);
int len=strlen(a);
for(int j=;j<len;j++)
{
if(a[j]=='h')
{
e[i][]=;
}
if(a[j]=='a')
e[i][]=;
if(a[j]=='r')
e[i][]=;
if(a[j]=='b')
e[i][]=;
if(a[j]=='i')
e[i][]=;
if(a[j]=='n')
e[i][]=; }
}

The 2019 China Collegiate Pro gramming Contest Harbin Site (F. Fixing Banners)的更多相关文章

  1. The 2019 China Collegiate Programming Contest Harbin Site

    题解: https://files.cnblogs.com/files/clrs97/HarbinEditorialV2.zip Code: A. Artful Paintings /* let x= ...

  2. The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners

    链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word ...

  3. The 2019 China Collegiate Programming Contest Harbin Site K. Keeping Rabbits

    链接: https://codeforces.com/gym/102394/problem/K 题意: DreamGrid is the keeper of n rabbits. Initially, ...

  4. The 2019 China Collegiate Programming Contest Harbin Site J. Justifying the Conjecture

    链接: https://codeforces.com/gym/102394/problem/J 题意: The great mathematician DreamGrid proposes a con ...

  5. The 2019 China Collegiate Programming Contest Harbin Site I. Interesting Permutation

    链接: https://codeforces.com/gym/102394/problem/I 题意: DreamGrid has an interesting permutation of 1,2, ...

  6. 模拟赛小结:The 2019 China Collegiate Programming Contest Harbin Site

    比赛链接:传送门 上半场5题,下半场疯狂挂机,然后又是差一题金,万年银首也太难受了. (每次银首都会想起前队友的灵魂拷问:你们队练习的时候进金区的次数多不多啊?) Problem J. Justify ...

  7. 2019 China Collegiate Programming Contest Qinhuangdao Onsite

    传送门 D - Decimal 题意: 询问\(\frac{1}{n}\)是否为有限小数. 思路: 拆质因子,看是不是只包含2和5即可,否则除不尽. Code #include <bits/st ...

  8. 2019 China Collegiate Programming Contest Qinhuangdao Onsite F. Forest Program(DFS计算图中所有环的长度)

    题目链接:https://codeforces.com/gym/102361/problem/F 题意 有 \(n\) 个点和 \(m\) 条边,每条边属于 \(0\) 或 \(1\) 个环,问去掉一 ...

  9. 2019 Multi-University Training Contest 1 String(序列自动机+贪心)

    题意 链接:https://vjudge.net/problem/HDU-6586 给你一个字符串和k,还有每个字符出现次数的限制,求一个长度为k的字典序最小的满足限制的子序列. 思路 先构造出序列自 ...

随机推荐

  1. Ubuntu安装Orcale

    Linux_Ubuntu安装oracle总结 ---------转自 https://www.2cto.com/database/201305/215338.html 话说我花了一晚上才在ubuntu ...

  2. CRC校验算法详解

    CRC(Cyclic Redundancy Check)循环冗余校验是常用的数据校验方法,讲CRC算法的文章很多,之所以还要写这篇,是想换一个方法介绍CRC算法,希望能让大家更容易理解CRC算法. 先 ...

  3. bzoj 4652: [Noi2016]循环之美

    额,,网上一堆题解,,随便一找就找到笨蒟蒻扒的了. 这个比较神奇的是纯循环小数就是[(y,k)=1],题解有证明这个的,貌似就是k进制下的类似循环节,不会不会.. 然后这道题就变成了求这个东西:∑(x ...

  4. 【C】揭秘rand()函数;

    原文地址:http://www.cnblogs.com/ngnetboy/archive/2012/11/23/2784078.html 相信只要是程序猿都会知道rand()函数是用来取随机数的一个库 ...

  5. Day5 - C - Agri-Net POJ - 1258

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...

  6. 把自己的项目发布到maven仓库并在maven和gradle中开始使用

    把自己的项目发布到maven仓库并在maven和gradle中开始使用 上一条博客中提到的日志打印项目总算是维护的差不多了, 不过现在使用它还是打成jar包放到其他项目内, 所以决定把项目传到mave ...

  7. C++代写,代写C++,C++程序代写,C++ assignment代写

    C++代写,代写C++,C++程序代写 关于C++代写,我们的涉猎范围: C++数据结构.算法题目 C++操作系统os题目 C++网络编程networking题目 C++ Linux题目 C++ Wi ...

  8. excel表格数据导入导出

    /** * 导出数据到excel表格 * Created by shenjianhua on 2018-12-28 */ package com.luer.comm.excel; import jav ...

  9. 利用ZXing.Net生成和识别二维码

    ZXing.Net:ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库. github:https://github.com/micjahn/ZXing.Net 直接将字符 ...

  10. HihoCoder第九周 状态压缩 二 与POJ2411总结

    在此我向各位博友求助,特别想知道除了HihoCoder上面的结果要对1e9+7取余之外,这两道题还有什么其他的问题,都是骨牌覆盖问题,都是状态压缩+dp,为什么我能过poj2411的程序过不了Hiho ...