1550: Simple String
Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 481 Solved: 211
[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
题意:给出3个串A B C,长度都为 2*n ,能否在 A 中选n个字符,在B中选n个字符,组成C?
题解:我们将A串作为基准串 ,A中每个字符能够选的最少数量为 max(0,num2[i]-num1[i]) ,能够选的最多数量为 min(num[i],num2[i]) ,将所有的可能相加,如果 n 在这个范围内,那么我们就一定可以在B中选出长度为n的串组成C。
#include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int N = 100005; char A[N],B[N],C[N]; int num[30],num1[30],num2[30]; int main() { while(scanf("%s",A)!=EOF){ scanf("%s",B); scanf("%s",C); int len = strlen(A); memset(num,0,sizeof(num)); memset(num1,0,sizeof(num1)); memset(num2,0,sizeof(num2)); for(int i=0;i<len;i++){ num[A[i]-'A']++; num1[B[i]-'A']++; num2[C[i]-'A']++; } bool flag = true; int l = 0,r = 0; for(int i=0;i<26;i++){ if(num[i]+num1[i]<num2[i]){ ///第三个串中某个字符数量 > 一串和二串之和 flag = false; break; } int MIN = max(0,num2[i]-num1[i]); ///最少可以贡献的字符数量 int MAX = min(num[i],num2[i]); ///最多可以贡献的字符数量 l+=MIN; r+=MAX; } if(!(l<=len/2&&r>=len/2)) flag = false; if(!flag) printf("NO "); else printf("YES "); } return 0; }