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

解题思路

水,,,

AC代码

#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
char s[1005], r[1005];
int main()
{
    scanf("%s%s", s, r);
    char *p = s;
    map<char, int> mp;
    while (*p){
        ++mp[*p++];
    }
    p = r;
    while (*p){
        --mp[*p++];
    }
    p = r;
    int cnt = 0;
    while (*p){
        if (mp[*p] < 0) {
            cnt -= mp[*p];
            mp[*p] = 0;
        }
        ++p;
    }
    if (cnt > 0){
        printf("No %d\n", cnt);
    }else{
        printf("Yes %d\n", strlen(s) - strlen(r));
    }
    return 0;
}
时间: 2024-07-29 01:09:07

1092. To Buy or Not to Buy (20)【水题】——PAT (Advanced Level) Practise的相关文章

1084. Broken Keyboard (20)【字符串操作】——PAT (Advanced Level) Practise

题目信息 1084. Broken Keyboard (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen. Now given a string that

1011. World Cup Betting (20)——PAT (Advanced Level) Practise

题目信息: 1011. World Cup Betting (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise

题目信息 1081. Rational Sum (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum. Input Specification: Each input file contains one test case. Each case star

1050. String Subtraction (20)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1050. String Subtraction (20) 时间限制10 ms 内存限制65536 kB 代码长度限制16000 B Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 - S2 for any giv

1041. Be Unique (20)【水题】——PAT (Advanced Level) Practise

题目信息 1041. Be Unique (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1, 104]. The first on

1015. Reversible Primes (20) ——PAT (Advanced Level) Practise

题目信息: 1015. Reversible Primes (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73

1027. Colors in Mars (20)【进制转换】——PAT (Advanced Level) Practise

题目信息 1027. Colors in Mars (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Re

1008. Elevator (20)——PAT (Advanced Level) Practise

题目信息: 1008. Elevator (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator w

1001. A+B Format (20) ——PAT (Advanced Level) Practise

题目信息: 1001. A+B Format (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than