F - Fixing Banners ccpc哈爾濱重現賽 DFS 或者全排列

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
Output
No
Yes
思路:
先將每行字符串處理一遍,統計都有哪些字母,在構造一個6x6的矩陣,DFS是否可行

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e6+10;
bool st[7];//標記有哪些字母已經用過了
int mp[10][10];//矩陣
bool dfs(int x)//遍歷第x行有沒有可行的情況
{
	if(x==5)
	{
		for(int i=0;i<6;i++)
		{
			if(mp[x][i]&&!st[i])
				return 1;
		}
			return 0;
	}
	for(int i=0;i<6;i++)
		if(mp[x][i]&&!st[i])//存在某個字母且沒標記
		{
			st[i]=1;
			if(!dfs(x+1))
				st[i]=0;//恢復現場
			else
			{
				return 1;
			}
		}
	return 0;
}
int main()
{
	char s[maxn],x;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int num[27];
		memset(mp,0,sizeof mp);
		bool flag=0,fag=0;
		for(int i=0;i<6;i++)
		{
			memset(num,0,sizeof num);
			scanf("%s",s);
			int len=strlen(s);
			for(int j=0;j<len;j++)//構造矩陣
			{
				if(s[j]=='h') mp[i][0]=1;
				if(s[j]=='a')  mp[i][1]=1;
				if(s[j]=='r')  mp[i][2]=1;
				if(s[j]=='b')  mp[i][3]=1;
				if(s[j]=='i')  mp[i][4]=1;
				if(s[j]=='n')  mp[i][5]=1;
			}
		}
		memset(st,0,sizeof st);
		for(int i=0;i<6;i++)
			if(mp[0][i])
			{
				st[i]=1;
				if(dfs(1))
				{
					flag=1;
					break;
				}
				st[i]=0;//一定要記得恢復現場! 切記!!!
			}
		if(flag)
			puts("Yes");
		else
			puts("No");
	}
	return 0;
}```
或者 預處理後 從harbin的字典序最小的情況開始遍歷,如果存在每一行各有一個不同的字母即爲可行輸出即可,如果不存在即爲不可行
```cpp
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e6+10;
int main()
{
	int t,mp[6][30];
	scanf("%d",&t);
	while(t--)
	{
		memset(mp,0,sizeof mp);
		for(int i=0;i<6;i++)
		{
			char st[maxn];
			scanf("%s",st);
			int len=strlen(st);
			for(int j=0;j<len;j++)
				mp[i][st[j]-'a']++;
		}
		char s[10]={"abhinr"};//一定要字典序最小才能遍歷所有情況
		bool flag;
		do
		{
			flag=1;
			for(int i=0;i<6;i++)
				if(!mp[i][s[i]-'a'])
					flag=0;
			if(flag)
				break;
		}while(next_permutation(s,s+6));
		if(flag)
			puts("Yes");
		else
			puts("No");
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章