Problem Description
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 *. 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
Input2 welcome toparticipate inthe ccpccontest inharbin inoctober harvest belong ninja reset amazing intriguingOutput
No Yes
思路是DFS。从每个字符串中取一个判断能否拼出harbin即可。
AC代码如下:
#include<iostream> #include<string> #include<cstring> using namespace std; int vis[6],m[7][7],flag=0; void dfs(int i){ if(i==6){ flag=1; return; } else{ for(int k=0;k<6;k++){ if(!vis[k]&&m[i][k]==1){ vis[k]=1; dfs(i+1); vis[k]=0; } } } return; } int main(){ int t; cin>>t; while(t--){ flag=0; memset(vis,0,sizeof(vis)); memset(m,0,sizeof(m)); string s[7]; for(int i=0;i<6;i++){ cin>>s[i]; int l=s[i].size(); for(int j=0;j<l;j++) if(s[i][j]=='h') m[i][0]=1; else if(s[i][j]=='a') m[i][1]=1; else if(s[i][j]=='r') m[i][2]=1; else if(s[i][j]=='b') m[i][3]=1; else if(s[i][j]=='i') m[i][4]=1; else if(s[i][j]=='n') m[i][5]=1; } dfs(0); if(flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }