PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)
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)的更多相关文章
- PAT (Basic Level) Practise:1039. 到底买不买
[题目链接] 小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有 ...
- PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20)
PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20) http://www.patest.cn/contests/pat-b-practise/1032 ...
- 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 ...
- PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)
PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
PAT (Basic Level) Practise (中文)-1025. 反转链表 (25) http://www.patest.cn/contests/pat-b-practise/1025 ...
- PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)
PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15) http://www.patest.cn/contests/pat-b-practise/10 ...
- PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)
PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20) http://www.patest.cn/contests/pat-b-practise/1027 本题 ...
- PAT (Basic Level) Practise (中文)-1028. 人口普查(20)
PAT (Basic Level) Practise (中文)-1028. 人口普查(20) http://www.patest.cn/contests/pat-b-practise/1028 某 ...
- PAT (Basic Level) Practise (中文)-1029. 旧键盘(20)
PAT (Basic Level) Practise (中文)-1029. 旧键盘(20) http://www.patest.cn/contests/pat-b-practise/1029 旧键盘上 ...
随机推荐
- 基于unity3d游戏的android版本逆向初探
https://bbs.pediy.com/thread-212532.htm [文章标题]: 基于unity3d游戏的android版本逆向初探 [文章作者]: dreaman [作者邮箱]: [e ...
- php UTF8 转字节数组,后使用 MD5 计算摘要
Hex.encodeHexString(md5.digest);按 UTF8 转字节数组,后使用 MD5 计算摘要,得到 16 字节数组,使用 Hex 转为长度为 32 的字符串,保持小写 bin2h ...
- 2014-10-22 NOIP模拟赛
1 1 .传教士 (bishop) 问题描述:panzhili 王国的疆土恰好是一个矩形,为了管理方便,国王 jjs 将整个疆土划分成 N*M 块大小相同的区域.由于 jjs 希望他的子民也能信教爱教 ...
- [软件工程基础]Alpha 软件测试报告
PhyLab Alpha 测试报告 测试中发现的bug Alpha版本限制与问题 由于接手时数据库已经丢失,这一版本主要修复了大部分数据库,使得网站得以运行. 相比接手时网站的状况,有以下改进: 恢复 ...
- LNMP下使用Phabricator(一)
首先是安装. 安装过程并不复杂,英文看得懂的可以自己看原文 https://secure.phabricator.com/book/phabricator/article/installation_g ...
- 学习flask的网址
学习flask的网址: http://www.bjhee.com
- linux网卡软中断shell脚本
LANG=C;export LANG; service irqbalance stop >/dev/null 2>&1;chkconfig irqbalance off; bon ...
- [转]eclipse启动tomcat无法访问的解决方法
这篇文章介绍了eclipse启动tomcat无法访问的解决方法,有需要的朋友可以参考一下 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080 ...
- Windows2
windows如何打开dvd, iso镜像文件 .iso后缀的文件是一个压缩文件, 使用Winrar等压缩工具即可打开 windows7如何下载Visual Studio 2010(2010是流行的开 ...
- kie-api 组件介绍
KieServices:kie整体的入口,可以用来创建Container,resource,fileSystem等 KieContainer: KieContainer就是一个KieBase的容器,可 ...