博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法题:到底买不买
阅读量:4183 次
发布时间:2019-05-26

本文共 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/

你可能感兴趣的文章
CareerCup Binary Tree the Maximum of 人
查看>>
CareerCup Divide n cakes to k different people
查看>>
CareerCup Randomly return a node in a binary tree
查看>>
CareerCup Given a sorted array which contains scores. Write a program to find occurrence
查看>>
CareerCup The number of pairs (x, y) of distinct elements with condition x + y <= Threshold
查看>>
Build a key-value data structure which can perform lookup and rangeLookup(key1, key2)
查看>>
整数划分问题---动态规划、递归
查看>>
Balanced Partition
查看>>
Number of 1s
查看>>
CareerCup Find all the conflicting appointments from a given list of n appointments.
查看>>
CareerCup Given an array having positive integers, find a subarray which adds to a given number
查看>>
CareerCup Generate all the possible substrings
查看>>
CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
查看>>
Brain Teaser 球变色
查看>>
(2)考试大纲---信息系统项目管理师考试系列
查看>>
(3)教材目录---信息系统项目管理师考试系列
查看>>
商城基础E-R模型图
查看>>
飞翔的小鸟--键盘事件案例
查看>>
一个sql函数group_concat详解
查看>>
根据地址返回坐标位置的百度地图api
查看>>