D - Simple String(字符统计&&细节)

D - Simple String

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu

Submit Status

Description

Welcome,this is the 2015 3th Multiple Universities Programming Contest ,Changsha ,Hunan Province. In order to let you feel fun, ACgege will give you a simple problem. But is that true? OK, let’s enjoy it.
There are three strings A , B and C. The length of the string A is 2*N, and the length of the string B and C is same to A. You can take N characters from A and take N characters from B. Can you set them to C ?

Input

There are several test cases.
Each test case contains three lines A,B,C. They only contain upper case letter.
0<N<100000
The input will finish with the end of file.

Output

For each the case, if you can get C, please print “YES”. If you cann’t get C, please print “NO”.

Sample Input

AABB
BBCC
AACC
AAAA
BBBB
AAAA

Sample Output

YES
NO

【分析】因为要分别从A和B中拿N长度的串来组成2N长度的C,所以可以统计A和B和C串中各个字符的个数,如果A或B中某字符的长度大于N,就再把它赋值为N,因为最多只能拿出N个字符。如果对于某字符c,A中个数+B中个数<C中个数,就不满足题意,break掉;注意题上给的范围是N的,所以开数组的时候要×2;

!!!!!!!!!YES和NO是大写!!!!因为这个问题WA了5次!!!!!(╬◣д◢)!!

【代码】

#include<bits/stdc++.h>
using namespace std;
char a[200005],b[200005],c[200005];
int aa[100],bb[100],cc[100];
int main()
{
	while(~scanf("%s%s%s",a,b,c))
	{
		int len=strlen(a),flag=1;
		memset(aa,0,sizeof(aa));
		memset(bb,0,sizeof(bb));
		memset(cc,0,sizeof(cc));
		for(int i=0;i<len;i++)
			aa[a[i]-'A']++;
		for(int i=0;i<len;i++)
			bb[b[i]-'A']++;
		for(int i=0;i<len;i++)
			cc[c[i]-'A']++;
		for (int i=0; i<26; i++)
        {
            if (aa[i] > len/2)aa[i] = len/2; //当A中某个字母>N中,最多也只能提供N个,赋值为N
            if (bb[i] > len/2)bb[i] = len/2;
            if (cc[i] > aa[i]+bb[i]){
            	flag=0;break;
			}
        }
        if(flag)cout<<"YES\n";
        else cout<<"NO\n";
	}
	return 0;
}

【补】这样写交的时候A了,但是小伙伴发现了问题——————

a串:ABCDEF

b串:DDEEFF

c串:ABCEEE

这一组数据其实是不可以的,但是按上面代码的思路做的话结果是YES。。。所以咯  问题就来了

判断某个字符出现的次数是否小于N不重要,重要的是两个串中,某个字符最多和最少需要多少并与len/2进行比较;

#include<bits/stdc++.h>
using namespace std;
char a[200005],b[200005],c[200005];
int aa[28],bb[28],cc[28];
int main()
{
	while(~scanf("%s",a))
	{
		int len=strlen(a),flag=1;
		memset(aa,0,sizeof(aa));
		memset(bb,0,sizeof(bb));
		memset(cc,0,sizeof(cc));
		for(int i=0;i<len;i++)
			aa[a[i]-'A']++;
		scanf("%s",b);
		for(int i=0;i<len;i++)
			bb[b[i]-'A']++;
		scanf("%s",c);
		for(int i=0;i<len;i++)
			cc[c[i]-'A']++;
		int nmax=0,nmin=0;
		for (int i=0; i<26; i++)
        {
         //   if (aa[i] > len/2)aa[i] = len/2;//当A中某个字母>N中,最多也只能提供N个,赋值为N
          //  if (bb[i] > len/2)bb[i] = len/2;
            if (cc[i] > aa[i]+bb[i]){
            	flag=0;break;
			}
			nmin+=max(0,cc[i]-aa[i]);//对于某个字母,B中最少要用多少个(C串需要数目减去A串已有数目与0取最大值)
            nmax+=min(bb[i],cc[i]);//对于某个字母,B中最多要用多少(取B串与C串出现次数小的那个)
        }
        if(flag)
        {
            if(nmax>=len/2 && nmin<=len/2)//在[nmin,nmax]范围内找到len/2才能构成C串,因为B串只需要拿出len/2个字母,总数目为len/2
                flag=1;
            else flag=0;
        }
        if(flag)cout<<"YES\n";
        else cout<<"NO\n";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/81807209
今日推荐