【CSU1550】Simple String(思维)

题目链接

1550: Simple String

Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: 661     Solved: 279    


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

Hint

Source

解题思路:

用A,B,C数组分别记录3个字符串中所出现字母的个数,因为题意要求A,B中各出n个字符看是否能构造成C,那么只需要求B中所出字母的最小值和最大值,看l/2是否在这个范围之内,若在,说明存在A种出n个字符,B中出n个字符构造成C。

B中所出字母的最小值即计算0与C[i]-A[i]的最大值即可,因为如果A足够出C需要的字符,那么B最少出0个字符,如果不够那么B需要出C[i]-A[i]个字符。

B中所出字母的最大值即计算B[i]与C[i]的最小值即可,因为如果B足够出C小的字符,那么B只要出C[i]个字符,如果不够那么B最多也只能出B[i]个字符。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e+5;
int A[maxn],B[maxn],C[maxn];
char s1[maxn],s2[maxn],s3[maxn];
int main()
{
    while(~scanf("%s%s%s",s1,s2,s3))
    {
        memset(A,0,sizeof(A));
        memset(B,0,sizeof(B));
        memset(C,0,sizeof(C));
        int l=strlen(s1);
        for(int i=0;i<l;i++)
            A[s1[i]-'A']++;
        for(int i=0;i<l;i++)
            B[s2[i]-'A']++;
        for(int i=0;i<l;i++)
            C[s3[i]-'A']++;
        int nmax=0,nmin=0,flag=1;
        for(int i=0;i<26;i++)
        {
            if(A[i]+B[i]<C[i])
            {
                flag=0;
                break;
            }
            nmin+=max(0,C[i]-A[i]);//B中最少要用多少字母
            nmax+=min(B[i],C[i]);//B中最多要用多少字母
        }
        if(flag)
        {
            if(nmax>=l/2 && nmin<=l/2)//在[nmin,nmax]范围内找到l/2才能构成C串
                flag=1;
            else flag=0;
        }
        if(flag)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/81807854