csu 1550: Simple String (字符串)

23 篇文章 0 订阅
10 篇文章 0 订阅


1550: Simple String

Time Limit: 1 Sec   Memory Limit: 256 MB
Submit: 249   Solved: 112
[ Submit][ Status][ Web Board]

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

题意:

题目的意思是给你两个长度为2*N的字符串,A,B,然后给定一个2*N长的字符串C,C字符串分别由A,B中的N个字符串组成,判断给定的两个字符串能否组成C。

分析:

由题意,我们就可以列出9种情况:判断c中的某个字符串是否符合题意。

因为A,B中的某个字母最多拿出N个去组成C,(当A,B字符串中的某个字母如果大于N个时,也最多只能拿出N个出来)这样才能满足组成C字符串的条件。


A[I]<NA[I]=NA[I]>N
B[I]<NC[I]<=A+BC[I]<=A+BC[I]<=N+B

B[I]=N

C[I]<=A+BC[I]<=A+BC[I]<=B+N
B[I]>NC[I]<=A+NC[I]<=A+NC[I]<=N+N

下面是ac 的代码:

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int A[26], B[26], C[26];
bool flag[26];
char str[200002];
int main()
{
    int i, lenth;
    while (cin >> str)
    {
        memset(A, 0, sizeof(A));
        memset(B, 0, sizeof(B));
        memset(C, 0, sizeof(C));
 
        lenth = strlen(str);
        for (i=0; i<lenth; i++)
        {
            A[str[i]-'A']++;//统计A字符串中某个字母的个数
        }
 
        cin >> str;
        for (i=0; i<lenth; i++)
        {
            B[str[i]-'A']++;//统计B字符串中字母的个数
        }
 
        cin >> str;
        for (i=0; i<lenth; i++)
        {
            C[str[i]-'A']++;
        }
 
        memset(flag, true, sizeof(flag));
        for (i=0; i<26; i++)
        {
            if (A[i] > lenth/2)
            {
                A[i] = lenth/2; //当A中某个字母>N中,最多也只能提供N个,赋值为N
            }
            if (B[i] > lenth/2)
            {
                B[i] = lenth/2;
            }
 
            if (C[i] <= A[i] + B[i])//当c字符串满足是,标记为false
            {
                flag[i] = false;
            }
 
        }
        for (i=0; i<26; i++)
        {
            if (flag[i] == true)//c字符串中有一个不满足,跳出循环
            {
                break;
            }
        }
        if (i<26)
        {
            cout << "NO" << endl;
        }
        else
        {
            cout << "YES" << endl;
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值