您的位置:首页 > 其它

湖南多校对抗赛3.28 D Simple String

2015-04-05 16:23 369 查看

<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: