题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805374509498368
思路很简单,利用一个数组记录各个颜色蛋糕的数目,因为颜色只用[0-9] [a-z] [A-Z]来表示,因此可以直接采取 [字符-‘0’] 的方式得到下标,从而建立存储位置和数目的映射关系,然后依据题意进行模拟即可。
#include <iostream>
using namespace std;
int main() {
int shop[100]={0};
string breads;
cin >> breads;
string buy;
cin >> buy;
for(int i=0;i<breads.size();i++){
shop[breads[i]-'0']++;
}
int has=0;
for(int i=0;i<buy.size();i++){
if(shop[buy[i]-'0']>0){
shop[buy[i]-'0']--;
has++;
}
}
if(has==buy.size()) cout << "Yes " << breads.size() - buy.size();
else cout << "No " << buy.size()-has;
return 0;
}