The 2019 China Collegiate Programming Contest Harbin Site

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 2⋅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

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

outputCopy

No
Yes

本题思路很简单,给6行字符串,从每行字符串中取出一个字符,构成“harbin”即可。
本题在搜索的时候,得采用DFS的方法,否则会一直超时。

#include
#include
#include
#include
#include
#include
using namespace std;
int map[6][6];
int vis[6];
int cnt, flag;
void dfs(int i)
{
	if (cnt == 6)
		flag = 1;
	if (flag == 1)
		return;
	if (flag == 0)
		return;
	int f = 0;
	for (int j = 0; j < 6; j++)
	{
		if (map[i][j] && !vis[j])
		{
			vis[j]++;
			cnt++;
			dfs(i + 1);
			vis[j]--;
			cnt--;
		}
		if (map[i][j])
			f = 1;
	}
	if (!f)
		flag = 0;
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		memset(map, 0, sizeof(map));
		memset(vis, 0, sizeof(vis));
		for (int ii = 0; ii < 6; ii++)
		{
			string s;
			cin >> s;
			for (int i = 0; i < s.size(); i++)
			{
				if (s[i] == 'h')
					map[ii][0] = 1;
				if (s[i] == 'a')
					map[ii][1] = 1;
				if (s[i] == 'r')
					map[ii][2] = 1;
				if (s[i] == 'b')
					map[ii][3] = 1;
				if (s[i] == 'i')
					map[ii][4] = 1;
				if (s[i] == 'n')
					map[ii][5] = 1;
			}
		}
		flag = 2;
		cnt = 0;
		dfs(0);
		if (flag==1)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}

你可能感兴趣的:(ACM,C++)