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. AngularJS进阶(三)HTML:让表单、文本框只读,不可编辑的方法

    HTML:让表单.文本框只读,不可编辑的方法 有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name=" ...

  2. Makefile的obj-y 和 obj-m

    目标定义是Kbuild Makefile的主要部分,也是核心部分.主要是定义了要编 译的文件,所有的选项,以及到哪些子目录去执行递归操作. 最简单的Kbuild makefile 只包含一行: 例子: ...

  3. Leetcode_223_Rectangle Area

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46868363 Find the total area co ...

  4. RTMPdump(libRTMP) 源代码分析 6: 建立一个流媒体连接 (NetStream部分 1)

    ===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...

  5. Android模拟器启动不了解决办法

    问题描述:Windows2008中的MyEclipse项目在Windows2003中运行时无法启动模拟器. 解决要点:启动模拟器管理工具,在启动中设置属性中不勾选默认尺寸显示. 系统错误如下: [20 ...

  6. PLSQL表

    PL/SQL表 一,什么是PL/SQL表? 首先PL/SQL表和记录(Record)一样,都是复合数据类型.可以看做是一种用户自定义数据类型. PL/SQL表由多列单行的标量构成的临时索引表对象.组成 ...

  7. 使用kubeadm搭建Kubernetes(1.10.2)集群(国内环境)

    目录 目标 准备 主机 软件 步骤 (1/4)安装 kubeadm, kubelet and kubectl (2/4)初始化master节点 (3/4) 安装网络插件 (4/4)加入其他节点 (可选 ...

  8. 解读Raft(四 成员变更)

    将成员变更纳入到算法中是Raft易于应用到实践中的关键,相对于Paxos,它给出了明确的变更过程(实践的基础,任何现实的系统中都会遇到因为硬件故障等原因引起的节点变更的操作). 显然,我们可以通过sh ...

  9. jdk1.7 tomcat-7安装

    由于软件下载地址经常有变动,所以不能直接wget,还是直接到网上点击下载 下载jdk http://www.oracle.com/technetwork/java/javase/downloads/j ...

  10. Mybatis解决jdbc编程的问题

    1.1.1  Mybatis解决jdbc编程的问题 1.  数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMapConfig.xml中配置 ...