PAT (Basic Level)1039. 到底买不买

来源:互联网 发布:新开淘宝店信誉 编辑:程序博客网 时间:2024/06/16 22:29

https://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:
ppRYYGrrYBR2258YrR8RrY
输出样例1:
Yes 8
输入样例2:
ppRYYGrrYB225YrR8RrY
输出样例2:
No 2
最喜欢映射了。

#include <iostream>#include <string>#include <algorithm> #include <cmath>#include <iomanip>#include <ctype.h>using namespace std;int index ( char ch){if ( ch >= '0' && ch <= '9') return ch-'0' ;else if ( ch >= 'a' && ch <= 'z') return 10+(ch-'a') ;else if ( ch >= 'A' && ch <= 'Z') return 36+(ch-'A') ;else return 63;}int main(){int c[65] = {0}, l1 = 0, l2 = 0, count = 0, i = 0;string in = "", need = "";cin >> in >> need ;l1 = in.length(), l2 = need.length();for ( i = 0 ; i < l1 ; i++){c[index(in[i])] ++;}for ( i = 0 ; i < l2 ; i++){int ind = index(need[i]);c[ind] --;if (c[ind] < 0) count ++;}if ( count > 0 ) printf("No %d", count);else printf("Yes %d", l1-l2);return 0;}


0 0
原创粉丝点击