题目链接
思路借鉴了类似1043 输出patest 对每个字符记录;
分别记录提供的每一个字符的每个个数以及实际需要的字符的个数;
运用循环,如果提供的字符串对应字符的个数>实际需要字符串的字符的个数,则为多余;记录和
如果小于,用另外一个值来记录。最后根据这个值的大小确定是否购买;
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#pragma warning(disable:4996);
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
char act[1005];
char need[1005];
int a[128] = { 0 };
int b[128] = { 0 };
cin >> act;
getchar();
cin >> need;
int l1 = strlen(act);
int l2 = strlen(need);
for (int i = 0;i < l1;i++)
a[act[i]]++;//实际的
for (int i = 0;i < l2;i++)
b[need[i]]++;//需要的
int ans1 = 0;//记录多余
int ans2 = 0;//记录缺损并且同时判断是否亏损;
for (int i = 0;i < 128;i++)
{
if (b[i] <=a[i]) ans1 += (a[i] - b[i]);
else ans2 += (b[i] - a[i]);
}
if (ans2 == 0) cout << "Yes " << ans1;
else cout << "No " << ans2;
}