zoukankan      html  css  js  c++  java
  • 1550: Simple String (做得少的思维题,两个字符串能否组成另外一个字符串问题)

    1550: Simple String

          Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: 682     Solved: 287    


    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
    长度都是2*n
    问你从a和b中各抽出n给字符
    能不能组成c
    分析:
    统计a,b,c中26个字符出现的次数
    如果某字符在a中出现次数+在b中出现次数小于该字符在c中出现次数
    那么肯定不能组成c
    1.A+B 与C的交集必须>=n
    2.A与C的交集必须>=n/2,B与C的交集必须>=n/2。
    理解上面两句话就可以了
     
    #include<stdio.h>
    #include<iostream>
    #include<vector>
    #include <cstring>
    #include <stack>
    #include <cstdio>
    #include <cmath>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    #include<string>
    #include<string.h>
    #include<math.h>
    typedef long long LL;
    using namespace std;
    #define max_v 125
    int c1[30];
    int c2[30];
    int c3[30];
    void init()
    {
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        memset(c3,0,sizeof(c3));
    }
    int main()
    {
        string str1,str2,str3;
        while(cin>>str1)
        {
            init();
            cin>>str2;
            cin>>str3;
            int l=str1.length();
            for(int i=0; i<l; i++)
            {
                c1[str1[i]-'A']++;
                c2[str2[i]-'A']++;
                c3[str3[i]-'A']++;
            }
            int flag=1;
            int v1=0;
            int v2=0;
            for(int i=0; i<26; i++)
            {
                if(c1[i]+c2[i]<c3[i])
                {
                    flag=0;
                    break;
                }
                v1+=max(0,c3[i]-c1[i]);
                v2+=min(c3[i],c2[i]);
            }
            if(v2<l/2||v1>l/2)
                flag=0;
            if(flag)
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;
    }
    /*
    题目意思:
    给你三个字符串
    a,b,c
    长度都是2*n
    问你从a和b中各抽出n给字符
    能不能组成c
    
    分析:
    统计a,b,c中26个字符出现的次数
    如果某字符在a中出现次数+在b中出现次数小于该字符在c中出现次数
    那么肯定不能组成c
    1.A+B 与C的交集必须>=n
    2.A与C的交集必须>=n/2,B与C的交集必须>=n/2。
    理解上面两句话就可以了
    
    */
  • 相关阅读:
    [BZOJ]1042 硬币购物(HAOI2008)
    [Codeforces]605E Intergalaxy Trips
    [Codeforces]860E Arkady and a Nobody-men
    [Codeforces]856E Satellites
    [Codeforces]849E Goodbye Souvenir
    [Codeforces]852I Dating
    [BZOJ]1095 Hide捉迷藏(ZJOI2007)
    洛谷3275 & BZOJ2330:[SCOI2011]糖果——题解
    洛谷5960:【模板】差分约束算法——题解
    The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9498622.html
Copyright © 2011-2022 走看看