您的位置:首页 > 其它

PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)

2015-04-20 14:48 316 查看
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[1000];
gets(str);

int istr=0,charnum[128]={0};
while(str[istr]) charnum[str[istr]]++,istr++;   // count beads which belong to the shop owner

gets(str);
istr=0;
while(str[istr]) charnum[str[istr]]--,istr++; // count beads which Eva need

int more=0,less=0;
for(int i=0;i<128;i++)
{
if(charnum[i]>0) more+=charnum[i];
else if(charnum[i]<0) less-=charnum[i];
}

if(less) printf("No %d",less);
else printf("Yes %d",more);
return 0;
}


第一次刷题

 #include<stdio.h>
#include<string.h>

// ppRYYGrrYBR2258 YrR8RrY Yes 8// ppRYYGrrYB225 YrR8RrY No 2
int main()
{
char str[1000];
gets(str);

int istr=0,charnum[200]={0};
while(str[istr])
{
if( ('0'<=str[istr] && str[istr]<='9')|| ('A'<=str[istr] && str[istr]<='Z') ||('a'<=str[istr] && str[istr]<='z'))
charnum[str[istr]]++;
istr++;
}

gets(str);
istr=0;
while(str[istr])
{
if( ('0'<=str[istr] && str[istr]<='9')|| ('A'<=str[istr] && str[istr]<='Z') ||('a'<=str[istr] && str[istr]<='z'))
charnum[str[istr]]--;
istr++;
}

int duo=0,shao=0;
for(int i=0;i<200;i++)
{
if( ('0'<=i && i<='9')|| ('A'<=i&& i<='Z') ||('a'<=i && i<='z'))
{
if(charnum[i]>0) duo+=charnum[i];
else shao+=charnum[i];
}
}
if(shao) printf("No %d",0-shao);
else printf("Yes %d",duo);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: