解题思路:此题用c语言的话可以直接hash去求比较简单,首先求出各个颜色的个数,一比对就可以了,python我利用了字典和集合
c语言代码:
#include<stdio.h>
int main()
{
char a[1001],b[1001];
int surplus=0,miss=0;
int a_c[128]={0};
int b_c[128]={0};
scanf("%s %s",a,b);
for(int i=0;a[i]!='\0';i++)
{
a_c[a[i]]++;
}
for(int i =0;b[i]!='\0';i++)
{
b_c[b[i]]++;
}
for(int i=0;i<128;i++)
{
if(a_c[i]>b_c[i])
surplus+=a_c[i]-b_c[i];
else
if(a_c[i]<b_c[i])
miss+=b_c[i]-a_c[i];
}
if(miss==0)
{
printf("Yes %d\n",surplus);
}
else
printf("No %d\n",miss);
return 0;
}
python版本:
surplus=0
miss=0
s=input()
a=input()
d_s={}
d_a={}
jihe=set()
for i in range(len(s)):
d_s.setdefault(s[i],s.count(s[i]))
jihe.add(s[i])
for i in range(len(a)):
d_a.setdefault(a[i],a.count(a[i]))
jihe.add(a[i])
for i in jihe :
s_count=d_s.get(i,0)
a_count=d_a.get(i,0)
if s_count>a_count:
surplus+=s_count-a_count
elif a_count>s_count:
miss+=a_count-s_count
if miss==0:
print('Yes',surplus)
else:
print('No',miss)