PAT (Advanced Level) 1092. To Buy or Not to Buy (20) 哈希
来源:互联网 发布:淘宝助理水印图片大小 编辑:程序博客网 时间:2024/06/07 22:58
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:ppRYYGrrYBR2258YrR8RrYSample Output 1:
Yes 8Sample Input 2:
ppRYYGrrYB225YrR8RrYSample Output 1:
No 2
用一个哈希表记录目标串s2每个字符出现的次数。遍历待检测串s1,若字符命中,该字符在哈希表中的次数减一,命中次数与s2长度相等时输出YES。
/*2015.7.31cyq*/#include <iostream>#include <string>#include <vector>#include <fstream>using namespace std;//ifstream fin("case2.txt");//#define cin finint main(){string s1,s2;cin>>s1>>s2;vector<int> h(256,0);int n=s1.size();int m=s2.size();for(int i=0;i<m;i++)h[s2[i]]++;int miss=m;for(int i=0;i<n;i++){if(h[s1[i]]>0){h[s1[i]]--;miss--;}}if(miss==0){cout<<"Yes "<<n-m;}else{cout<<"No "<<miss;}return 0;}
- PAT (Advanced Level) 1092. To Buy or Not to Buy (20) 哈希
- 1092. To Buy or Not to Buy (20)【水题】——PAT (Advanced Level) Practise
- 【PAT】【Advanced Level】1092. To Buy or Not to Buy (20)
- PAT-PAT (Advanced Level) Practise To Buy or Not to Buy(20) 【一星级】
- PAT (Advanced Level) Practise 1092 To Buy or Not to Buy (20)
- PAT (Advanced Level) Practise 1092 To Buy or Not to Buy (20)
- Pat(Advanced Level)Practice--1092(To Buy or Not to Buy)
- PAT A 1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy (20)
- pat 1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy (20)
- 【PAT】1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy (20)
- PAT-A 1092. To Buy or Not to Buy (20)
- PAT-A-1092. To Buy or Not to Buy (20)
- Pat(A) 1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy
- PAT 1092. To Buy or Not to Buy(STL)
- VS2010工程属性——生成后事件
- muduo::ThreadPoll分析
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
- Qt归纳(二)
- Android线程
- PAT (Advanced Level) 1092. To Buy or Not to Buy (20) 哈希
- NYOJ~106~背包问题~贪心算法~
- 两个css文件定义了同样的内容,后执行的会覆盖前面的
- 65. Valid Number
- Android Bundle传值
- 设置多个回车默认按钮
- C#中Find及Findindex用法
- 滴滴打车创新盈利方式
- 2015多校联合训练第三场Painter(hdu5319)