B1039. 到底买不买(20)

来源:互联网 发布:保定seo胜达 编辑:程序博客网 时间:2024/05/22 14:35

小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。

为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如在图1中,第3串是小红想做的珠串;那么第1串可以买,因为包含了全部她想要的珠子,还多了8颗不需要的珠子;第2串不能买,因为没有黑色珠子,并且少了一颗红色的珠子。

输入格式:

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

输出格式:

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

输入样例1:
ppRYYGrrYBR2258
YrR8RrY
输出样例1:
Yes 8
输入样例2:
ppRYYGrrYB225
YrR8RrY
输出样例2:
No 2

/* B1039*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>int chage(char c) {    if(c >= '0' && c <= '9') return c - '0';    if(c >= 'a' && c <= 'z') return c - 'a' + 10;    if(c >= 'A' && c <= 'Z') return c - 'A' + 36;}int main(){//为什么自己用统计hashTabel所有正数和负数的方法一个答案都没有通过呢?书上的简单例子可以通过啊???觉得思路没错啊?//居然是Yes 写成YES,晕死。。。。OJ提交一定要复制输出格式!!!!    int hashTabel[128]={0};    char str1[1010];    char str2[1010];//    char str1[]="ppRYYGrrYBR2258";//    char str2[]="YrR8RrY";    gets(str1);    gets(str2);    int str1Long=strlen(str1);    int str2Long=strlen(str2);    for(int i=0;i<str1Long;++i){//        int c1=str1[i];//        hashTabel[c1]++;        int id=chage(str1[i]);        hashTabel[id]++;    }    for(int j=0;j<str2Long;++j){//        int c2=str2[j];//        hashTabel[c2]--;        int id=chage(str2[j]);        hashTabel[id]--;    }    int positive=0;    int negative=0;    for(int k=0;k<128;k++){        if(hashTabel[k]>=0){            positive+=hashTabel[k];        }        else if(hashTabel[k]<0){            negative+=hashTabel[k];        }    }    if(positive>=0&&negative==0){        printf("Yes %d\n",positive);    }    else if(negative<0){        negative=abs(negative);        printf("No %d\n",negative);    }    return 0;}
0 0