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

旺 崽發表於2020-10-03

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

題目連結

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), 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 2e6.

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

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

output

No
Yes

這題直接暴力就行,求一個 “harbin” 的全排列,找每個字串是否有該字元即可,但是一定要預處理每個字串存在的字元,而且不能用 m a p map map,習慣 STL 的我哭了, m a p map map 會 T,unordered 我沒試過,估計也差不多,改成陣列就過了,DFS 的話也可以,不過 DFS 比較難寫,複雜度是一樣的,好像還有人用二分圖匹配的,那感覺就想複雜了?,AC程式碼如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
string target,s[6];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        target="abhinr";
        int flag=0;
        int m[6][26];
        memset(m,0,sizeof(m));
        for(int i=0;i<6;i++){
            cin>>s[i];
            for(auto j:s[i]) m[i][j-'a']=1;
        }
        do{
            int num=0;
            for(int i=0;i<6;i++){
                if(m[i][target[i]-'a']) num++;
            }
            if(num==6){
                flag=1;
                break;
            }
        }while(next_permutation(target.begin(),target.end()));
        if(flag) cout<<"Yes\n";
        else cout<<"No\n";
    }
    return 0;
}

相關文章