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;
}
时间: 2024-08-06 11:58:22

csu 1550: Simple String (字符串)的相关文章

Water --- CSU 1550: Simple String

Simple String Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1550 Mean: 略. analyse: 水题. Time complexity: O(n) Source code:  // Memory Time // 1347K 0MS // by : crazyacking // 2015-03-29-12.08 #include<map> #include<queue> #

1550: Simple String 最大流解法

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550 很久以前做的一题,当时队友用最大流做,现在我也是 这个转化为二分图多重匹配,就是一样的意思了. 设出一个原点S,两个人1,和2,S-->1的流量是n表明只能流出n个字母,s-->2的流量是n,一样含义. 然后再设一个汇点T,表明需要有多少流进来,跑一发就好. #include <bits/stdc++.h> #define IOS ios::sync_with_stdio

JAVA基础——重新认识String字符串

深入剖析Java之String字符串 在程序开发中字符串无处不在,如用户登陆时输入的用户名.密码等使用的就是字符串. 在 Java 中,字符串被作为 String 类型的对象处理. String 类位于 java.lang 包中.默认情况下,该包被自动导入所有的程序. 创建 String 对象有三种方法 String s1="我是字符串1"; String s2=new String();//创建一个空的字符串对象 String s3=new String("我是字符串2&q

Java String字符串/==和equals区别,str。toCharAt(),getBytes,indexOf过滤存在字符,trim()/String与StringBuffer多线程安全/StringBuilder单线程—— 14.0

课程概要 String 字符串 String字符串常用方法 StringBuffer StringBuilder String字符串: 1.实例化String对象 直接赋值  String str="Hello";  推荐这种 使用关键字new  String str1=new String("Hello"); 在内存中开辟2个空间 如图: 源代码 StringDemo01.java 2.String内容的比较 String str="Hello"

不使用java内置函数,将String字符串转换为int类型

package com.test; public class AtoiTest { public static void main(String[] args) throws Exception { String s = "-011134"; System.out.println("转换前的字符串:" + s); System.out.println("atoi1转换后的字符串:" + atoi1(s)); System.out.println(

ZOJ 1151 Word Reversal反转单词 (string字符串处理)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 For each list of words, output a line with each word reversed without changing the order of the words. This problem contains multiple test cases! The first line of a multiple input is

集合或数组转成String字符串

1.将集合转成String字符串 String s=""; for (int i = 0; i < numList.size(); i++) { if (s=="") { s=numList.get(i); }else { s=s+","+numList.get(i); } } 定义List集合,如: List<String> numList=new ArrayList<String>(); for(int i=1;

C++入门经典-例6.20-修改string字符串的单个字符

1:使用+可以将两个string 字符串连接起来.同时,string还支持标准输入输出函数.代码如下: // 6.20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main() { string str1 = "您好,"; string str2; cout<<&

C++入门经典-例6.21-比较string字符串,比较两个字符串

1:使用">"."!=".">="等比较运算符可以比较两个字符串的内容.比较的方法是将两个string字符串从头开始比较每一个字符,直到出现两者不一致.比较这两个不相同的字符的字面值,得出相应的结果.代码如下: // 6.21.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <string> u