PAT甲级1092 To Buy or Not to Buy :[C++题解]哈希表
生活随笔
收集整理的這篇文章主要介紹了
PAT甲级1092 To Buy or Not to Buy :[C++题解]哈希表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 題目分析
- 題目鏈接
題目分析
來源:acwing
分析
用unordered_map存每種珠子的個數,商店有的加上,伊娃需要的減去。這樣哈希表中存的就是每種珠子的凈值。大于0表示多余的,小于0表示不夠的,分別統計即可。
ac代碼
#include<bits/stdc++.h> using namespace std;int main(){string a, b;cin >> a >> b;unordered_map<char,int> S;for(auto c:a) S[c]++;for(auto c:b) S[c]--;int sp = 0,sn = 0; //多余的和不夠的for(auto item:S){if(item.second > 0) sp+= item.second;else sn -= item.second;}if(sn>0) cout<<"No"<<" "<<sn; //不夠的else cout<<"Yes"<<" "<< sp;}題目鏈接
PAT甲級1092 To Buy or Not to Buy
https://www.acwing.com/problem/content/1584/
總結
以上是生活随笔為你收集整理的PAT甲级1092 To Buy or Not to Buy :[C++题解]哈希表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级1083 List Grade
- 下一篇: PAT甲级1129 Recommenda