csu 1550(字符串处理思路题)

本文解析了一道关于从两个字符串中选取字符以匹配第三个字符串的编程竞赛题目。通过计算每个字符在目标字符串中可能出现的最小和最大次数,验证是否可以从源字符串中选择合适的字符来构成目标字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1550: Simple String

Time Limit: 1 Sec  Memory Limit: 256 MB
Submit: 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\n");
        else printf("YES\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5792562.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值