本文共 1454 字,大约阅读时间需要 4 分钟。
题目描述:
小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一
下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。
为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以买,因为包含了
全部她想要的珠子,还多了8颗不需要的珠子;ppRYYGrrYB225不能买,因为没有黑色珠子,并且少了一颗红色的珠子。
输入描述:
每个输入包含1个测试用例。每个测试用例分别在2行中先后给出摊主的珠串和小红想做的珠串,两串都不超过1000个珠子。
输出描述:
如果可以买,则在一行中输出“Yes”以及有多少多余的珠子;如果不可以买,则在一行中输出“No”以及缺了多少珠子。其间以1个空格分隔。
输入例子:
ppRYYGrrYBR2258YrR8RrY
输出例子:
Yes 8
代码实现如下:
# define _CRT_SECURE_NO_WARNINGS#include#include #include using namespace std;int main(){ char s1[] = { 0 }; char s2[] = { 0 }; cin >> s1 ; cin >> s2; int Dist[] = { 0 }; int Src[] = { 0 }; char *p1 = s1; char*p2 = s2; while (*p1 != '\0') { Dist[*p1]++; p1++; } while (*p2 != '\0') { Src[*p2]++; p2++; } int count = 0; //计算差数 //差数=strlen(s1) - strlen(s2) //////////////////////////////////////////// bitset<256> bt; p2 =s2; while (*p2 != '\0') { bt.set(*p2); p2++; } /////////////////////////////////////////// for (int i = 0; i<256; i++) { if (bt.test(i)) { if (Src[i]>Dist[i]) count += (Src[i] - Dist[i]); } } if (count == 0)//说明Dist满足条件,有可能多了。 { cout << "Yes" << " " << strlen(s1) - strlen(s2) << endl; } else { cout << "No" << " " << count << endl; } return 0;}
转载地址:http://ctuoi.baihongyu.com/