1092 To Buy or Not to Buy (PAT甲级)

news/2024/11/28 8:23:03/

1092. To Buy or Not to Buy (20)-PAT甲级真题_柳婼的博客-CSDN博客

柳婼的解法要更清晰一些。

下面是我的解法……

#include <iostream>
#include <string>int main(){std::string a, b;bool flag = true;int extra, missing;int cntA[62] = {0};int cntB[62] = {0};std::cin >> a >> b;for(int i = 0; i < a.size(); ++i){if(a[i] >= '0' && a[i] <= '9'){cntA[a[i] - '0']++;} else if(a[i] >= 'a' && a[i] <= 'z'){cntA[a[i] - 'a' + 10]++;} else{cntA[a[i] - 'A' + 36]++;}}for(int i = 0; i < b.size(); ++i){if(b[i] >= '0' && b[i] <= '9'){cntB[b[i] - '0']++;} else if(b[i] >= 'a' && b[i] <= 'z'){cntB[b[i] - 'a' + 10]++;} else{cntB[b[i] - 'A' + 36]++;}}extra = 0;missing = 0;for(int i = 0; i < 62; ++i){if(cntA[i] > cntB[i]){extra += cntA[i] - cntB[i];} else if(cntA[i] < cntB[i]){flag = false;missing += cntB[i] - cntA[i];}}std::cout << (flag ? "Yes " : "No ") << (flag ? extra : missing);return 0;
}

题目如下:

Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is Yes, please tell her the number of extra beads she has to buy; or if the answer is No, please tell her the number of beads missing from the string.

For the sake of simplicity, let's use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. For example, the 3rd string in Figure 1 is the one that Eva would like to make. Then the 1st string is okay since it contains all the necessary beads with 8 extra ones; yet the 2nd one is not since there is no black bead and one less red bead.

Figure 1

Input Specification:

Each input file contains one test case. Each case gives in two lines the strings of no more than 1000 beads which belong to the shop owner and Eva, respectively.

Output Specification:

For each test case, print your answer in one line. If the answer is Yes, then also output the number of extra beads Eva has to buy; or if the answer is No, then also output the number of beads missing from the string. There must be exactly 1 space between the answer and the number.

Sample Input 1:

ppRYYGrrYBR2258
YrR8RrY

Sample Output 1:

Yes 8

Sample Input 2:

ppRYYGrrYB225
YrR8RrY

Sample Output 2:

No 2

 


http://www.ppmy.cn/news/259560.html

相关文章

【区块链 | L2】详解Layer 2扩展解决方案Optimism(OP)是如何工作的?

Optimism是一个快速、稳定、可扩展和低成本的以太坊Layer 2区块链。这意味着它在以太坊区块链(Layer1)之上运行,以帮助缓解拥塞,进而降低交易成本和处理时间。作为现有以太坊软件的最小扩展,Optimism的EVM-equivalent 架构可以扩展以太坊应用程序。如果它能在以太坊上工作…

郑州中科集成电路与系统应用研究院面试

简历中的项目自我介绍论文中的创新点介绍YOLOv3和YOLOv5的区别Yolov5解码介绍YOLOx和以往YOLO系列的区别正负样本怎么选检测算法评价指标P,R,mAP详细含义Anchor free or Anchor baseFocal loss介绍U-netU-net评价指标介绍Kmeans介绍Faster rcnnAnchor 怎么计算对mmdetection的了…

展会邀请|虹科诚邀您3月29-31日参加深圳国际传感器与应用技术展览会!

01 展会介绍 深圳国际传感器与应用技术展览会&#xff08;Shenzhen International Sensor Technology Expo 2023&#xff09;将于2023年3月29-31日于深圳会展中心&#xff08;福田&#xff09;举办&#xff0c;届时展会规模在原有基础上将扩大一倍&#xff0c;专业技术含量和产…

2022IC秋招面经分享【禾赛科技·FPGA开发工程师(上海)】

【流程】 2022.8.18 一面 2022.8.25 二面 面经分享 一面 1.讲项目 2.笔试的除法器怎么实现的&#xff1f; 讲了流水线乘法器&#xff0c;用移位的方法&#xff0c;面试官说还有查表&#xff0c;逼近的办法 3.PAPR算法被抑制的点在接收机怎么处理&#xff1f; 我一直觉得是…

我在中关村给不了你国贸的爱

我住在海淀区&#xff0c;这里满大街跑的是月薪上万的小程序员儿&#xff0c;还有痛骂计算机专业无美女的项目经理&#xff0c;他们大脑袋、厚眼镜、因熬夜而布满血丝的眼睛&#xff0c;因不见阳光而 酷 似吸血鬼的脸&#xff0c;穿一个星期不洗的T-SHIRT&#xff0c;很有很有味…

2022IC秋招面经分享【格科微电子·数字电路设计工程师(上海)】

【流程】 2022.8.6 笔试 2022.8.19 一面 面经分享 格科微的笔试感觉挺难的&#xff0c;而且时间好像只有一小时&#xff0c;反正有点来不及做&#xff0c;不过还好过了 一面 1.讲项目 2.OFDM是怎么产生的 3.讲述一下SPI总线基本原理 4.项目中接收机AD下来怎么接收数据 回答…

Java实战和面试宝典

这是一篇记录工作和学习过程中遇到的一些问题的文章&#xff0c;每个问题都很有代表性&#xff0c;值得不断回顾和深入思考的&#xff0c;答案未必十分准确&#xff0c;但可以作为一种参考&#xff0c;共勉&#xff01; 读了这篇不一样的八股文&#xff0c;你将至少拥有三年经…

在Windows中安装极狐GitLab Runner(解决 Host key verification failed.)

官方文档&#xff1a;https://docs.gitlab.cn/runner/install/windows.html 在Windows中安装极狐GitLab Runner 1.下载GitLab Runner二进制文件&#xff0c;新建一个文件夹&#xff08;不要有中文&#xff09;&#xff0c;并将二进制文件放入该文件夹&#xff0c;重命名为“git…