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串不能买,因为没有黑色珠子,并且少了一颗红色的珠子。
![](http://www.patest.cn/upload/gx_nkqhj1eeck3.jpg)
图 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.
![](http://www.patest.cn/upload/gz_nkqi5eg6wio.jpg)
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信息->根据是否缺少进行输出
第二次刷题:
1 #include<cstdio> 2 #include<cstring> 3 4 5 int main() 6 { 7 char str[1000]; 8 gets(str); 9 10 int istr=0,charnum[128]={0}; 11 while(str[istr]) charnum[str[istr]]++,istr++; // count beads which belong to the shop owner 12 13 gets(str); 14 istr=0; 15 while(str[istr]) charnum[str[istr]]--,istr++; // count beads which Eva need 16 17 int more=0,less=0; 18 for(int i=0;i<128;i++) 19 { 20 if(charnum[i]>0) more+=charnum[i]; 21 else if(charnum[i]<0) less-=charnum[i]; 22 } 23 24 if(less) printf("No %d",less); 25 else printf("Yes %d",more); 26 return 0; 27 }
第一次刷题
1 #include<stdio.h> 2 #include<string.h> 3 4 // ppRYYGrrYBR2258 YrR8RrY Yes 8 5 // ppRYYGrrYB225 YrR8RrY No 2 6 7 8 int main() 9 { 10 char str[1000]; 11 gets(str); 12 13 int istr=0,charnum[200]={0}; 14 while(str[istr]) 15 { 16 if( ('0'<=str[istr] && str[istr]<='9')|| ('A'<=str[istr] && str[istr]<='Z') ||('a'<=str[istr] && str[istr]<='z')) 17 charnum[str[istr]]++; 18 istr++; 19 } 20 21 22 gets(str); 23 istr=0; 24 while(str[istr]) 25 { 26 if( ('0'<=str[istr] && str[istr]<='9')|| ('A'<=str[istr] && str[istr]<='Z') ||('a'<=str[istr] && str[istr]<='z')) 27 charnum[str[istr]]--; 28 istr++; 29 } 30 31 int duo=0,shao=0; 32 for(int i=0;i<200;i++) 33 { 34 if( ('0'<=i && i<='9')|| ('A'<=i&& i<='Z') ||('a'<=i && i<='z')) 35 { 36 if(charnum[i]>0) duo+=charnum[i]; 37 else shao+=charnum[i]; 38 } 39 } 40 if(shao) printf("No %d",0-shao); 41 else printf("Yes %d",duo); 42 return 0; 43 44 }