【PAT甲级】1092 To Buy or Not to Buy (20分):哈希

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805374509498368

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


注意

  • 谨记一句话“所有变量使用前都要初始化赋值。

经常碰到因为没初始化导致运算结果出错的情况,在C++中局部变量使用前一定要手动初始化,全局变量会默认初始化,但记来记去容易搞混,全部初始化吧。


代码

#include<iostream>

using namespace std;

int main() {
    string s1,s2;//店主、Eva的字符串
    int time[256];//数组记录 字符出现次数
    int result=0;//缺珠子的个数
    cin>>s1;
    getchar();
    cin>>s2;

    fill(time,time+256,0);
    for(int i=0; i<s1.length(); i++) {
        time[s1[i]]++;
    }
    for(int i=0; i<s2.length(); i++) {
        if(time[s2[i]]>0) {
            time[s2[i]]--;//珠子给Eva
        } else { //缺珠子
            result++;
        }
    }

    if(result!=0) {//缺珠子
        cout<<"No "<<result;
    } else //不缺珠子 ,输出要多买珠子的数量
        cout<<"Yes "<<s1.length()-s2.length();

    return 0;
}

原文地址:https://www.cnblogs.com/musecho/p/12287031.html

时间: 2024-07-29 15:18:43

【PAT甲级】1092 To Buy or Not to Buy (20分):哈希的相关文章

【PAT甲级】1079 Total Sales of Supply Chain (25 分)

题意: 输入一个正整数N(<=1e5),表示共有N个结点,接着输入两个浮点数分别表示商品的进货价和每经过一层会增加的价格百分比.接着输入N行每行包括一个非负整数X,如果X为0则表明该结点为叶子结点接着输入一个整数表示该零售商进货的数量,X不为零则接着输入X个正整数表示它的下级经销商是哪些结点.输出所有零售商进货的总价.(结点从0~N-1,0为根节点即供应商) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using name

【PAT甲级】1097 Deduplication on a Linked List (25 分)

题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点组成的链表). AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int nex[100007],val[100007]; 5 vector<

【PAT甲级】1099 Build A Binary Search Tree (30 分)

题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 pair<int,int>a[107]; 5 int b[107]; 6 int cnt=0; 7 int ans[107]

PAT 乙级 1083 是否存在相等的差(20 分)

1083 是否存在相等的差(20 分) 给定 N 张卡片,正面分别写上 1.2.--.N,然后全部翻面,洗牌,在背面分别写上 1.2.--.N.将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差? 输入格式: 输入第一行给出一个正整数 N(2 ≤ N ≤ 10 000),随后一行给出 1 到 N 的一个洗牌后的排列,第 i 个数表示正面写了 i 的那张卡片背面的数字. 输出格式: 按照"差值 重复次数"的格式从大到小输出重复的差值及其重复的次数,每行输出一

【PAT】1083 是否存在相等的差(20 分)

//这题不是我耍流氓,实在太简单,只能直接贴代码了,凑个数 #include<stdio.h> int aaa[10005]={0}; int main(){ int N;scanf("%d",&N);//卡片数 for(int i=1;i<=N;i++){//循环输入数据,一面为下标,一面为元素内容 int temp; scanf("%d",&temp); int a=abs(temp-i); aaa[a]++; } for(in

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

#include<cstdio> #include<malloc.h> #include<algorithm> int arr[101]; void Priarr(int a,int b){ if(a<=b) for(int i=a;i<=b;i++){ if(i!=a)printf(" "); printf("%d",arr[i]); } } int main(){ int N,M; scanf("%d%d

PAT B1022 D进制的A+B (20 分)

输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+B 的 D 进制数. 输入样例: 123 456 8 输出样例: 1103 #include <cstdio> #include <iostream> //const int MAXN = 100000; //int score[MAXN] = { 0 }; int main() { int a, b, c,

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

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 w