PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039

小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。

为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如在图1中,第3串是小红想做的珠串;那么第1串可以买,因为包含了全部她想要的珠子,还多了8颗不需要的珠子;第2串不能买,因为没有黑色珠子,并且少了一颗红色的珠子。


图 1

输入格式:

每个输入包含1个测试用例。每个测试用例分别在2行中先后给出摊主的珠串和小红想做的珠串,两串都不超过1000个珠子。

输出格式:

如果可以买,则在一行中输出“Yes”以及有多少多余的珠子;如果不可以买,则在一行中输出“No”以及缺了多少珠子。其间以1个空格分隔。

输入样例1:

ppRYYGrrYBR2258
YrR8RrY

输出样例1:

Yes 8

输入样例2:

ppRYYGrrYB225
YrR8RrY

输出样例2:

No 2  

PAT(A) 101-125-1-2015-03-14 A. To Buy or Not to Buy (20)  http://www.patest.cn/contests/pat-a-101-125-1-2015-03-14/A

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 1:

No 2

这个题的题目信息可能让读者误以为是字符(颜色)匹配,实际是有效信息量处理。
有效信息包括摊主提供的串珠都什么颜色、每个颜色有多少颗;小红需要的有什么颜色,每个颜色多少颗。
eg: 如果小红想要的珠子中有七个红色的,那么摊主提供的串中的红色珠子至少得有七颗,否则No。

思路:计算摊主提供的串/小红需要的串中每个颜色的珠子的个数,最后对比,若对于每一个需要的颜色,摊主提供的>=小红需要的,则Yes,否则No。

继续分析:
对于Yes/No,每个颜色多少颗并不重要,只要摊主提供的该颜色的个数>=小红需要的即可。只要缺少值非零,则No;否则不缺。
对于要求输出的个数,如果缺少值非零,其有效数值即缺了多少珠子;否则,就输出多余的。
对于一个颜色,其珠子个数初始为0,统计摊主提供的整串珠子时,遇到一个则++;统计小红需要的珠子时,遇到一个--。

对于颜色, [0-9], [a-z], and [A-Z],分别是48-57,97-122,65-90。所以只需要一个10+26+26的数组就可满足统计需求。但是题目简单、数据量小,为了节省时间,这些不需要处理,直接开个大一点的数组,使用48~90的小标表示颜色即可;如果想不起来这些字符对应的ascii编号,也无所谓,反正最低不低于0、最高不高于127,所以直接来个int [128]既简单又省事。

统计摊主的串珠->减去小红想要的->统计more/less信息->根据是否缺少进行输出

第二次刷题:

 #include<cstdio>
#include<cstring> int main()
{
char str[];
gets(str); int istr=,charnum[]={};
while(str[istr]) charnum[str[istr]]++,istr++; // count beads which belong to the shop owner gets(str);
istr=;
while(str[istr]) charnum[str[istr]]--,istr++; // count beads which Eva need int more=,less=;
for(int i=;i<;i++)
{
if(charnum[i]>) more+=charnum[i];
else if(charnum[i]<) less-=charnum[i];
} if(less) printf("No %d",less);
else printf("Yes %d",more);
return ;
}

第一次刷题

 #include<stdio.h>
#include<string.h> // ppRYYGrrYBR2258 YrR8RrY Yes 8
// ppRYYGrrYB225 YrR8RrY No 2 int main()
{
char str[];
gets(str); int istr=,charnum[]={};
while(str[istr])
{
if( (''<=str[istr] && str[istr]<='')|| ('A'<=str[istr] && str[istr]<='Z') ||('a'<=str[istr] && str[istr]<='z'))
charnum[str[istr]]++;
istr++;
} gets(str);
istr=;
while(str[istr])
{
if( (''<=str[istr] && str[istr]<='')|| ('A'<=str[istr] && str[istr]<='Z') ||('a'<=str[istr] && str[istr]<='z'))
charnum[str[istr]]--;
istr++;
} int duo=,shao=;
for(int i=;i<;i++)
{
if( (''<=i && i<='')|| ('A'<=i&& i<='Z') ||('a'<=i && i<='z'))
{
if(charnum[i]>) duo+=charnum[i];
else shao+=charnum[i];
}
}
if(shao) printf("No %d",-shao);
else printf("Yes %d",duo);
return ; }

PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)的更多相关文章

  1. PAT (Basic Level) Practise:1039. 到底买不买

    [题目链接] 小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有 ...

  2. PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20)

    PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20) http://www.patest.cn/contests/pat-b-practise/1032 ...

  3. PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20)

    PAT (Basic Level) Practise (中文)-  1022. D进制的A+B (20)  http://www.patest.cn/contests/pat-b-practise/1 ...

  4. PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)

    PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...

  5. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  6. PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)

    PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)    http://www.patest.cn/contests/pat-b-practise/10 ...

  7. PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)

    PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)  http://www.patest.cn/contests/pat-b-practise/1027 本题 ...

  8. PAT (Basic Level) Practise (中文)-1028. 人口普查(20)

    PAT (Basic Level) Practise (中文)-1028. 人口普查(20)   http://www.patest.cn/contests/pat-b-practise/1028 某 ...

  9. PAT (Basic Level) Practise (中文)-1029. 旧键盘(20)

    PAT (Basic Level) Practise (中文)-1029. 旧键盘(20) http://www.patest.cn/contests/pat-b-practise/1029 旧键盘上 ...

随机推荐

  1. 《深入理解Java虚拟机》笔记02 -- 垃圾收集算法

    1. 标记 - 清除算法 先标记出所有需要回收的对象,在标记完成后统一回收所有被标记的对象.它是最基础的收集算法.其他收集算法都是根据其思路,改进其不足之处. 缺点:1) 标记和清除两个阶段的效率都不 ...

  2. 01分数规划初探?!By cellur925

    都要\(NOIp\)了为啥我还在看这种玄学玩意..... \(01\)分数规划:这是一个问题模型\(qwq\),一般是在求\[\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i ...

  3. hls流媒体视频防盗实现

    HLS流媒体视频防盗实现 一.Windows安装FFmpeg 1.1 安装版本 1.1.1 网址:https://ffmpeg.org/ 1.1.2 选择Windows版本:https://ffmpe ...

  4. JavaScript 与 CSS 滚动实现最新指南

    一些(网站)滚动的效果是如此令人着迷但你却不知该如何实现,本文将为你揭开它们的神秘面纱.我们将基于最新的技术与规范为你介绍最新的 JavaScript 与 CSS 特性,(当你付诸实践时)将使你的页面 ...

  5. c#字符串字面量

    分为两种: 1 常规字符串字面量 2逐字字面量字符串:以@字符为前缀.注意:注意逐字字面量唯一例外的是相邻的双引号组,它们被解释为单个双引号字符.

  6. [bzoj 1758] 重建计划

    bzoj 1758 重建计划 题意: 给定一棵有边权的树和两个数 \(L, R (L\leq R)\),求一条简单路径,使得这条路径经过的边数在 \(L, R\) 之间且路径经过的边的边权的平均值最大 ...

  7. win7设置管理员权限

    1.在运行中输入:secpol.msc 2.修改设置权限设置 3.在账户中, 将administrator启用并设置密码 将其他用户取消管理原权限,设置为user权限

  8. Java8中的新特性Optional

    Optional 类是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象.Optional 是个容器:它可以保存类型T的值,或者仅仅保存 ...

  9. 075 Sort Colors 分类颜色

    给定一个包含红色.白色和蓝色,且含有 n 个元素的数组,对它们进行排序,使得相同颜色的元素相邻,颜色顺序为红色.白色.蓝色.此题中,我们使用整数 0, 1 和 2 分别表示红色,白色和蓝色.注意:不能 ...

  10. abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一)

    在前面我已经介绍了ASP.NET MVC.ASP.NET Razor.WEBAPI等技术.我准备通过一个实践项目来整体应用一下之前介绍的技术.本系列是介绍基于ABP+EasyUI的Web开发框架的形成 ...