csu 1550: Simple String (字符串)



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]<N A[I]=N A[I]>N
B[I]<N C[I]<=A+B C[I]<=A+B C[I]<=N+B

B[I]=N

C[I]<=A+B C[I]<=A+B C[I]<=B+N
B[I]>N C[I]<=A+N C[I]<=A+N C[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;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章