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. sessionStorage二种存值取值的方法

    //方法一 sessionStorage.setItem('id1','这是一个测试id1'); //存入一个值key:value console.log(sessionStorage.getItem ...

  2. 51nod1107(逆序对数&归并排序)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1107 题意:中文题诶- 思路:通过题意可以发现对于两点p1(x ...

  3. 阿里云ECS 自己搭建 hyperledger fabric的错误

    常常有在本地搭建没问题,到阿里云上跑的时候 fabric启动不成功的问题. 引用: https://yq.aliyun.com/articles/238940 解决方案 1.在e2e_cli 下有个d ...

  4. spring-eureka 源码解读----为什么一个服务最多两分钟被其他服务感知

    Eureka的wiki上有一句话,大意是一个服务启动后最长可能需要2分钟时间才能被其它服务感知到,但是文档并没有解释为什么会有这2分钟.其实这是由三处缓存 + 一处延迟造成的. 首先,Eureka对H ...

  5. phpstorm使用

    生成注释快捷键/**+enter 注释快捷键ctrl+/

  6. css3椭圆运动

    通过使用css3实现让元素椭圆运动.而不是圆形运动. 效果1:http://sandbox.runjs.cn/show/ignefell 效果2:http://runjs.cn/code/w2wxjy ...

  7. [SDOI2013]随机数生成器

    Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. 注意 ...

  8. POJ1741(点分治)

    分治的时候SZ感觉是错的--但是貌似第一次找好重心就够了,之后SZ别太离谱就不会T,重心随一随缘就好-- #include <cstdio> #include <cstring> ...

  9. php:封装了个时间函数,返回类似“1分钟前发布”,“5小时前发布”,“3年前发布”

    处理和时间有关的时候,像发布问题等通常不会用date格式的时间,而是用类似"3分钟前发布"等格式,下面封装的php函数就可以使用: 注意:当有用到strtotime()函数的记得加 ...

  10. NET Core & Entity Framework Core

    ABP 教程文档 1-1 手把手引进门之 ASP.NET Core & Entity Framework Core(官方教程翻译版 版本3.2.5)   本文是ABP官方文档翻译版,翻译基于 ...