1092. To Buy or Not to Buy (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is "Yes", please tell her the number of extra beads she has to buy; or if the answer is "No", please tell her the number of beads missing from the string.

For the sake of simplicity, let's use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. For example, the 3rd string in Figure 1 is the one that Eva would like to make. Then the 1st string is okay since it contains all the necessary beads with 8 extra ones; yet the 2nd one is not since there is no black bead and one less red bead.


Figure 1

Input Specification:

Each input file contains one test case. Each case gives in two lines the strings of no more than 1000 beads which belong to the shop owner and Eva, respectively.

Output Specification:

For each test case, print your answer in one line. If the answer is "Yes", then also output the number of extra beads Eva has to buy; or if the answer is "No", then also output the number of beads missing from the string. There must be exactly 1 space between the answer and the number.

Sample Input 1:

ppRYYGrrYBR2258
YrR8RrY

Sample Output 1:

Yes 8

Sample Input 2:

ppRYYGrrYB225
YrR8RrY

Sample Output 1:

No 2

思路
用map模拟一个字典。nomiss记录满足需求的珠子数,buy记录买来的项链上多余珠子数。
1.把需要的项链的珠子放进字典里并计数,如dic<珠子类别,珠子数>形式。
2.检查买来的项链上的每一个珠子,如果在字典里存在,则字典对应的珠子数减1,减1后如果为0则把对应珠子从字典里删掉。如果不存在buy++。
3.检查字典是否为空,为空表示买来的项链满足需求的项链,输出多买的珠子数量buy;不为空表示买来的项链的珠子类别没有满足需求的项链,输出需求项链缺失的珠子数量miss(miss = 需求项链的长度- nomiss)
代码
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
string shop,need;
while(cin >> shop >> need)
{
//Initialize
int buy = ;
int nomiss = ;
map<char,int> dic;
for(int i = ;i < need.size();i++)
{
if(dic.count(need[i]) > )
dic[need[i]]++;
else
dic.insert(pair<char,int>(need[i],));
}
for(int i = ;i < shop.size();i++)
{
if(dic.count(shop[i]) > )
{
if(--dic[shop[i]] == )
{
dic.erase(shop[i]);
}
nomiss++;
}
else
buy++;
}
if(dic.empty())
cout << "Yes" << " " << buy;
else
cout << "No" << " " << need.size() - nomiss;
}
}
 

PAT1092:To Buy or Not to Buy的更多相关文章

  1. PAT 1092 To Buy or Not to Buy

    1092 To Buy or Not to Buy (20 分)   Eva would like to make a string of beads with her favorite colors ...

  2. 1092 To Buy or Not to Buy (20 分)

    1092 To Buy or Not to Buy (20 分) Eva would like to make a string of beads with her favorite colors s ...

  3. poj1092. To Buy or Not to Buy (20)

    1092. To Buy or Not to Buy (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. pat 1092 To Buy or Not to Buy(20 分)

    1092 To Buy or Not to Buy(20 分) Eva would like to make a string of beads with her favorite colors so ...

  5. PAT_A1092#To Buy or Not to Buy

    Source: PAT A1092 To Buy or Not to Buy (20 分) Description: Eva would like to make a string of beads ...

  6. 1092. To Buy or Not to Buy (20)

    Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...

  7. A1092. To Buy or Not to Buy

    Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...

  8. PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)

    http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...

  9. PAT甲级——A1092 To Buy or Not to Buy【20】

    Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...

随机推荐

  1. C++开发基础

    硬件配置: 586以上PC兼容机或品牌机,配有彩色显示器.鼠标.键盘,内存不小于20MB,硬 盘自由空间不少于60MB.推荐配置为内存32MB或64MB(或以上),硬盘自由空间500MB 以上. 软件 ...

  2. Prefix tree

    Prefix tree The trie, or prefix tree, is a data structure for storing strings or other sequences in ...

  3. 开源项目AndroidReview学习小结(1)

    多看多学涨姿势 最近学习了一个开源项目,感觉收获颇多,这里做下简要的记录,首先感谢作者的开源.先看个大概图 感觉框架非常简单,界面也很一般,不过底层的处理的一些处理还是有很多可圈可点之处,代码的处理一 ...

  4. LeetCode(60)-ZigZag Conversion

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  5. ASP.NET Core 2.0 : 九.从Windows发布到CentOS的跨平台部署

    本文聊一下如何在Windows上用VS开发并发布, 然后将其部署到CentOS上.对于我们一些常在Windows上逛的来说,CentOS用起来还真有些麻烦.MSDN官方有篇文章大概讲了一下(链接),按 ...

  6. 大数据批量导入,解决办法,实践从定时从 sqlserver 批量同步数据到 mySql

    c#代码,批量导入数据代码 public class MySql_Target : ZFCommon.DataAccesser.Base.DABase { public MySql_Target() ...

  7. 浅入javascript正则表达式的规则.

    今天在看jQuery源码的时候,到处都是正则的用法,一气之下就狠下心来.重新回顾了一下正则.下面是做的笔记.非常浅的入门. /* i:表示不区分大小写 g:表示可以全局配置 m:表示可以多行配置 */ ...

  8. SpringBoot入门之简单配置

    今天下载了<JavaEE开发的颠覆者SpringBoot实战>这本书,发现Spring还有好多遗漏的部分,算是又恶补了一下,今天主要是学习下SpringBoot的配置. 一.基本配置 1. ...

  9. Day8 接口与归一化设计

    接口:在程序的使用中,我不能把程序的主体直接提供给使用者,一般是提供一个接口. 为什么要使用接口: 1,接口提取了一群共同的函数,可以把接口当做一个函数的集合. 2,让子类去实现接口中的函数. 归一化 ...

  10. [经验分享] OSCP 渗透测试认证

    http://f4l13n5n0w.github.io/blog/2015/05/05/jing-yan-fen-xiang-oscp-shen-tou-ce-shi-ren-zheng/ “120天 ...