Amusing Joke (水题)方法种类比较齐全

A. Amusing Joke
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests at last. When two “New Year and Christmas Men” meet, thear assistants cut out of cardboard the letters from the guest’s name and the host’s name in honor of this event. Then the hung the letters above the main entrance. One night, when everyone went to bed, someone took all the letters of our characters’ names. Then he may have shuffled the letters and put them in one pile in front of the door.
The next morning it was impossible to find the culprit who had made the disorder. But everybody wondered whether it is possible to restore the names of the host and his guests from the letters lying at the door? That is, we need to verify that there are no extra letters, and that nobody will need to cut more letters.
Help the “New Year and Christmas Men” and their friends to cope with this problem. You are given both inscriptions that hung over the front door the previous night, and a pile of letters that were found at the front door next morning.
Input
The input file consists of three lines: the first line contains the guest’s name, the second line contains the name of the residence host and the third line contains letters in a pile that were found at the door in the morning. All lines are not empty and contain only uppercase Latin letters. The length of each line does not exceed 100.
Output
Print “YES” without the quotes, if the letters in the pile could be permuted to make the names of the “New Year and Christmas Men”. Otherwise, print “NO” without the quotes.
Examples
input
Copy
SANTACLAUS
DEDMOROZ
SANTAMOROZDEDCLAUS
output
Copy
YES
input
Copy
PAPAINOEL
JOULUPUKKI
JOULNAPAOILELUPUKKI
output
Copy
NO
input
Copy
BABBONATALE
FATHERCHRISTMAS
BABCHRISTMASBONATALLEFATHER
output
Copy
NO
Note
In the first sample the letters written in the last line can be used to write the names and there won’t be any extra letters left.
In the second sample letter “P” is missing from the pile and there’s an extra letter “L”.
In the third sample there’s an extra letter “L”.
题意:第一行和第二行的字符串任意合并,去看最后的字符串是不是第一行和第二行字母的任意合并
题解:直接用string去合并排序后,即可通过strcmp去比较合并后的字符串和最后一行字符串,只要是0,就相等
简单技巧:

#include<bits/stdc++.h>
using namespace std;
string s,s2,s3;
int main( )
{
    
    
	        cin>>s>>s2>>s3;
			string s1=s+s2;
			sort(s1.begin(),s1.end());
			sort(s3.begin(),s3.end());
			if(s1==s3)
			cout<<"YES"<<endl;
			else 
			cout<<"NO"<<endl;
		return 0;
}

常规方法://分别检测各个字母的出现频率,在最后的字符串中去递减即可,查看数字是不是全部归零

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    char string1[101],string2[101],string[202];
    int a[26],b[26];
    int k=0,c=0;
    memset(a,0,sizeof(a));//数组初始化
    memset(b,0,sizeof(b));// 数组初始化
   gets(string1);
    gets(string2);
    gets(string);
    int l1=strlen(string1);
    int l2=strlen(string2);
    int l=strlen(string);
	    for(int i=0;i<l1;i++)
	    {
    
    
	        a[string1[i]-'A']++;
	    }
	    for(int i=0;i<l2;i++)
	    {
    
    
        a[string2[i]-'A']++;
  	    }
	    for(int i=0;i<l;i++)
	    {
    
    
	        b[string[i]-'A']++;
	    }
	    bool flag=true;
	    for(int i=0;i<26;i++)
	    {
    
    
	        if(a[i]!=b[i])
	        {
    
    
	            flag=false;
	            break;
	        }
	    }
	    if(flag)
	       cout<<"YES"<<endl;
	    else  cout<<"NO"<<endl;
	  //  system("pause");
	  }
//上述代码段也可以改成如下所示:
int d=0for(d=0;d<26;d++)
{
    
    
    if(a[d]!=b[d])
         break;
  }
  if(d==26)
      cout<<"YES"<<endl;
   else
       cout<<"NO"<<endl;
 //再者而言,也可以通过在第一第二个字符串中的频率在第三个字符串中去递减,只要有一个数组没有全部归零,就是不符合题意的
``

猜你喜欢

转载自blog.csdn.net/weixin_46006714/article/details/109774311
今日推荐