The 2019 China Collegiate Pro gramming Contest Harbin Site (F. Fixing Banners)

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 (1T50000)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 21062⋅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']=1;
    id['a']=2;
    id['r']=3;
    id['b']=4;id['i']=5;id['n']=6;

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

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

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

    那么接下来就是DFS了

void dfs(int idx)
{
    if(ok)
        return ;
    if(idx==7)
    {
        ok=1;return ;
    }
    for(int i=1;i<=6;i++)
    {
        if(!vis[i]&&e[i][idx])
        {
            vis[i]=true;
            dfs(idx+1);
            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+10;
using namespace std;
char s[maxn];
int e[10][10];
bool vis[70];
int id[130];
int ok=0;
void dfs(int idx)
{
    if(ok)
        return ;
    if(idx==7)
    {
        ok=1;return ;
    }
    for(int i=1;i<=6;i++)
    {
        if(!vis[i]&&e[i][idx])
        {
            vis[i]=true;
            dfs(idx+1);
            vis[i]=false;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    id['h']=1;
    id['a']=2;
    id['r']=3;
    id['b']=4;id['i']=5;id['n']=6;
    while(t--)
    {
        memset(e,0,sizeof(e));
        ok=0;
        for(int i=1;i<=6;i++)
        {
            scanf("%s",s);
            vis[i]=false;
            int len=strlen(s);
            for(int j=0;j<len;j++)
            {
                e[i][id[s[j]]]=1;
            }
        }
        dfs(1);
        if(ok)
            printf("Yes
");
        else
            printf("No
");
    }
    return 0;
}

另一个录入方法:

  

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

原文地址:https://www.cnblogs.com/liyexin/p/11799631.html