珠串买不买

输入格式:

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

输出格式:

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

输入样例1:

ppRYYGrrYBR2258
YrR8RrY
  • 1
  • 2
  • 3

输出样例1:

Yes 8
  • 1
  • 2

输入样例2:

ppRYYGrrYB225
YrR8RrY
  • 1
  • 2
  • 3

输出样例2:

No 2
#include <iostream>    
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 1010;
int hashs[100] = { 0 };
int miss = 0;
int change(char c)   //将数字和字母转换为hashs的下标
{
	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() {
       	
	char whole[maxn], target[maxn];
	gets(whole);
	gets(target);
	int len1 = strlen(whole);
	int len2 = strlen(target);
	for (int i = 0; i < len1; i++)  //遍历第一个串
	{
		int id = change(whole[i]);  //字符转化为hashs的下标
		hashs[id]++;    //该颜色个数加1
	}
	for (int i = 0; i < len2; i++)  //遍历第二个串
	{
		int id = change(whole[i]);
		hashs[id]--;
		if (hashs[id] < 0)
			miss++;
	}
	if (miss>0) 
                 cout << "NO" << miss << endl;  //有缺少
	else 
                 cout << "Yes" << len1 - len2 << endl;  //有多余
	 
	return 0;
}
 

  

 

posted @ 2018-08-05 18:03  道微真理  阅读(103)  评论(0)    收藏  举报