pat 1092. To Buy or Not to Buy
来源:互联网 发布:淘宝u站今日特惠 编辑:程序博客网 时间:2024/05/18 03:07
原题链接:https://www.patest.cn/contests/pat-a-practise/1092
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 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
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int a[280],b[280];char s[1005];int main(){ int i,j,len; scanf("%s",s); len=strlen(s); for(i=0;i<len;i++) a[s[i]]++; scanf("%s",s); len=strlen(s); for(i=0;i<len;i++) b[s[i]]++; int cnt1=0,cnt2=0; int flag=0; for(i=0;i<=256;i++) { if(b[i]>a[i]) { flag=1; cnt2+=b[i]-a[i]; } cnt1+=a[i]-b[i]; } if(flag) printf("No %d\n",cnt2); else printf("Yes %d\n",cnt1); return 0;}
- 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 (20)
- pat 1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy(STL)
- 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
- PAT--1092. To Buy or Not to Buy
- 【PAT】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
- PAT-A-1092. To Buy or Not to Buy (20)
- Pat(A) 1092. To Buy or Not to Buy (20)
- 1092.To Buy or Not to Buy
- 1092. To Buy or Not to Buy
- 1092. To Buy or Not to Buy
- (贪心算法)Wooden Sticks
- 关于FLASH的初步知识1
- 《月亮和六便士》读后感
- eclipse新建android项目运行提示“Conversion to Dalvik format failed with error 1”
- C4.5决策树预测销量
- pat 1092. To Buy or Not to Buy
- web开发-web前端面试题(html-css-js)-学习笔记十二
- java中接口总结
- poj 2115 C Looooops (扩展欧几里德)
- PHP
- Android Support V4, V7, V13的作用与用法
- 【LeetCode91-100】编码种数,逆转单链表,IP地址转化,中序遍历二叉树,生成二叉搜索树,计算二叉树个数,交叉string【hard】,判断二叉搜索树是否合法,恢复二叉树(有两个元素被交换)
- 伪学习总结:匈牙利算法
- 有序查找算法(二分查找、差值查找、斐波那契查找)