湖南多校对抗赛3.28 D Simple String

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Problem D: Simple String</span>

Time Limit: 1 Sec  Memory Limit: 256 MB
Submit: 74  Solved: 35
[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

题意:有A,B,C三串字符串,取A,B串的各一半,问能不能组成C;

思路:分别记录A,B,C里面每个元素出现的个数,如果a[i] + b[i] < c[i],那么就直接输出NO;

用min_a记录a最少要拿出几个字母,用max_a记录a最多要拿出几个字母,如果len/2在min_a和max_a之间,就输出YES,否则输出NO。
代码如下:
#include<cstdio>
#include<cstring>
const int maxn=100005;
char A[maxn], B[maxn], C[maxn];
int a[maxn], b[maxn], c[maxn];
int main()
{
	while(scanf("%s%s%s", &A, &B, &C)!=EOF)
	{
		for(int i=0; i<26; i++)
		{
			a[i]=0, b[i]=0, c[i]=0;
		}
		int len=strlen(A);
		for(int i=0;  i<len; i++)
		{
			a[A[i]-'A']++;
		}
		for(int i=0;  i<len; i++)
		{
			b[B[i]-'A']++;
		}
		for(int i=0;  i<len; i++)
		{
			c[C[i]-'A']++;
		}
		int min_a=0, max_a=0;
		int flag=0;
		for(int i=0; i<26; i++)
		{
			if(a[i]+b[i]<c[i])
			{
				flag=1; 
				printf("NO\n");
				break;
			}
			else
			{
				if(c[i]>b[i])
				{
					min_a+=c[i]-b[i];
				}
				if(a[i]>c[i])
				{
					max_a+=c[i];
				}
				else max_a+=a[i];
			}
		}
		if(!flag)
		{
			int mid=len/2;
			if(mid>=min_a && mid<=max_a)
			{
				printf("YES\n");
			}
			else
			{
				printf("NO\n");
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值