算法初步—散列—A1092

思路:
1、引入一个hash数组来对应字符串字符和数量
2、统计超市已有的各个颜色数量
3、根据要求的各颜色数量,判断是否符合条件以及缺少多少

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
    char s1[1005],s2[1005];
    int hash[200] = {0};
    scanf("%s%s",s1,s2);
    int n1 = strlen(s1), n2 = strlen(s2);
    for(int i=0;i<n1;i++) //超市卖出的玻璃粉各颜色个数 
        hash[s1[i]]++;
    int loss = 0; 
    for(int i=0;i<n2;i++)
    {
        if(!hash[s2[i]]) //超市没有这种颜色,缺少个数+1 
            loss++;
        else            //超市有这种颜色,库存数量-1 
            hash[s2[i]]--;
    }
    if(!loss)
        printf("Yes %d",abs(n1-n2));
    else
        printf("No %d",loss);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/daidaihema/article/details/79215265