Simple String

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,C.每个字符串的长度相同,都是2*n。
从A,B串中每个串中最多选n个字符。问从A,B中选的
字符能否构成字符串C

设三个数组,分别记录三个字符串中每个字符的数量
再遍历由A,B字符串得到的数组,当字符的数量大于
n时,赋值为n;因为每一个字符串只能贡献n个字符
然后判断两个字符串的字符能否构成c
#include<string>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;

int as[26],bs[26],cs[26],sta[26];
int main()
{
    string str;
    while(cin>>str)
    {
        memset(as,0,sizeof as);
        memset(bs,0,sizeof bs);
        memset(cs,0,sizeof cs);
        int L=str.length();
        for(int i=0;i<L;i++){
            as[str[i]-'A']++;
        }
        cin>>str;
        for(int i=0;i<L;i++){
            bs[str[i]-'A']++;
        }
        cin>>str;
        for(int i=0;i<L;i++){
            cs[str[i]-'A']++;
        }
        int flag=0;memset(sta,0,sizeof sta);
        for(int i=0;i<26;i++)
        {
            if(as[i]>L/2)as[i]=L/2;
            if(bs[i]>L/2)bs[i]=L/2;
            if(cs[i]>as[i]+bs[i]){
                flag=1;break;
            }
        }
        printf(flag?"NO\n":"YES\n");
    }
}

猜你喜欢

转载自blog.csdn.net/Cworld2017/article/details/81807728