pat 1092 To Buy or Not to Buy(20 分)

1092 To Buy or Not to Buy(20 分)

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 2:

No 2
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <map>
 6 #include <stack>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #define LL long long
11 using namespace std;
12 const int MAX = 2e3 + 10;
13
14 char s1[MAX], s2[MAX];
15 map <char, int> mp1;
16 map <char, int> mp2;
17 map <char, int> :: iterator iter;
18 int cnt = 0;
19
20 int main()
21 {
22 //    freopen("Date1.txt", "r", stdin);
23     scanf("%s%s", &s1, &s2);
24     int len1 = strlen(s1), len2 = strlen(s2);
25     for (int i = 0; i < len1; ++ i)
26         mp1[s1[i]] ++;
27     for (int i = 0; i < len2; ++ i)
28         mp2[s2[i]] ++;
29
30     for (iter = mp2.begin(); iter != mp2.end(); ++ iter)
31     {
32         if (mp1.find(iter->first) == mp1.end())
33             cnt += iter->second;
34         else if (mp1[iter->first] < iter->second)
35             cnt += iter->second - mp1[iter->first];
36     }
37     if (cnt == 0)
38         printf("Yes %d\n", len1 - len2);
39     else
40         printf("No %d\n", cnt);
41     return 0;
42 }

原文地址:https://www.cnblogs.com/GetcharZp/p/9591494.html

时间: 2024-11-10 00:19:59

pat 1092 To Buy or Not to Buy(20 分)的相关文章

pat 1116 Come on! Let&#39;s C(20 分)

1116 Come on! Let's C(20 分) "Let's C" is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following: 0

PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their

PAT B1008 数组元素循环右移问题 (20 分)

一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A?0??A?1???A?N?1??)变换为(A?N?M???A?N?1??A?0??A?1???A?N?M?1??)(最后M个数循环移至最前面的M个位置).如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法? 输入格式: 每个输入包含一个测试用例,第1行输入N(1)和M(≥):第2行输入N个整数,之间用空格分隔. 输出格式: 在一行中输出循环右移M位以后的整数序列,之间用

【PAT甲级】1031 Hello World for U (20 分)

题意: 输入一个字符串长度为5~80,以'U'型输出,使得底端一行字符数量不小于侧面一列,左右两列长度相等. trick: 不把输出的数组全部赋值为空格为全部答案错误,可能不赋值数组里值为0,赋值后是' ',空格的ascii是32,初读题面时并没有看到要输出空格,因为打印0其实效果看起来好像一样... 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;string s;char a[87][87

PAT Basic 1078 字符串压缩与解压 (20 分)

文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符没有重复,就原样输出.例如 aba 压缩后仍然是 aba. 解压方法就是反过来,把形如 5c 这样的表示恢复为 ccccc. 本题需要你根据压缩或解压的要求,对给定字符串进行处理.这里我们简单地假设原始字符串是完全由英文字母和空格组成的非空字符串. 输入格式: 输入第一行给出一个字符,如果是 C 就表示下面的字符串需要被压缩:

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

1069 The Black Hole of Numbers (20 分) For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second

PAT Advanced 1065 A+B and C (64bit) (20分)

Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specification: The first line of the input gives the positive number of test cases, T (≤). Then T test cases follow, each consists of a single line containing three

PAT Advanced 1069 The Black Hole of Numbers (20分)

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in

1092. To Buy or Not to Buy (20)【水题】——PAT (Advanced Level) Practise

题目信息 1092. To Buy or Not to Buy (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 o