到底买不买(20)

来源:互联网 发布:西安软件培训班 编辑:程序博客网 时间:2024/05/19 22:07

小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一

下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。

为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以买,因为包含了

全部她想要的珠子,还多了8颗不需要的珠子;ppRYYGrrYB225不能买,因为没有黑色珠子,并且少了一颗红色的珠子。

输入描述:
每个输入包含1个测试用例。每个测试用例分别在2行中先后给出摊主的珠串和小红想做的珠串,两串都不超过1000个珠子。

输出描述:
如果可以买,则在一行中输出“Yes”以及有多少多余的珠子;如果不可以买,则在一行中输出“No”以及缺了多少珠子。其间以1个空格分隔。

输入例子:
ppRYYGrrYBR2258

YrR8RrY

输出例子:

Yes 8

#include<stdio.h>#include<stdlib.h>#include<string>#include<iostream>using namespace std;int main(void){    int a[2][64] = {0};    string str1;    string str2;    cin >> str1;    cin >> str2;    int len1 = str1.length();    int len2 = str2.length();    for (int i = 0; i < len1; i++)//母串    {        if (str1[i] <= 'z'&&str1[i] >= 'a')            a[0][str1[i] - 'a' + 36]++;        else if (str1[i] - '0' <= 9&&str1[i] - '0' >= 0)            a[0][str1[i] - '0']++;        else if (str1[i] <= 'Z'&&str1[i] >= 'A')            a[0][str1[i] - 'A' + 10]++;    }    for (int i = 0; i < len2; i++)//子串    {        if (str2[i] <= 'z'&&str2[i] >= 'a')            a[1][str2[i] - 'a' + 36]++;        else if (str2[i] - '0' <= 9 && str2[i] - '0' >= 0)            a[1][str2[i] - '0']++;        else if (str2[i] <= 'Z'&&str2[i] >= 'A')            a[1][str2[i] - 'A' + 10]++;    }    int shao = 0;    for (int i = 0; i < 64; i++)    {        if (a[1][i] != 0)        {              if (a[0][i]<a[1][i])                shao += (-a[0][i] + a[1][i]);        }    }    if (!shao)        printf("Yes %d\n",len1-len2);    else        printf("No %d\n",shao);    getchar();    return 0; }
0 0
原创粉丝点击