zoukankan      html  css  js  c++  java
  • F

    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;
    }
    
  • 相关阅读:
    最长连续子序列(dp,分而治之递归)
    判断线段是否相交
    1840: Jack Straws
    5065: 最长连续子序列
    TZOJ 4493: Remove Digits
    TZOJ 5271: 质因数的个数
    2019年天梯赛
    PTA 树的遍历
    TZOJ:3660: 家庭关系
    PTA 复数四则运算
  • 原文地址:https://www.cnblogs.com/neflibata/p/12871811.html
Copyright © 2011-2022 走看看