1092. To Buy or Not to Buy (20)

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
解析:本题同样采用HASH的思想,直接把数组元素值和数组元素位置之间建立索引,而不使用多重循环。

/*************************************************************************> File Name: 1092.c> Author: YueBo> Mail: yuebowhu@163.com> Created Time: Sun 14 May 2017 12:05:03 PM CST************************************************************************/#include<stdio.h>
#include <stdio.h>int main()
{int extra = 0, miss = 0, flag = 0;int shop_str[10+26+26];char ch;int i;for (i = 0; i < 10+26+26; i++)shop_str[i] = 0;while ((ch = getchar()) != '\n' && ch != ' '){if (ch >= '0' && ch <= '9')shop_str[ch-'0']++;else if (ch >= 'a' && ch <= 'z')shop_str[10+ch-'a']++;elseshop_str[10+26+ch-'A']++;}if (ch == ' '){while ((ch = getchar()) != '\n');}while ((ch = getchar()) != '\n' && ch != ' '){if (ch >= '0' && ch <= '9'){shop_str[ch-'0']--;if (shop_str[ch-'0'] < 0)flag = 1;}else if (ch >= 'a' && ch <= 'z'){shop_str[10+ch-'a']--;if (shop_str[10+ch-'a'] < 0)flag = 1;}else{shop_str[10+26+ch-'A']--;if (shop_str[10+26+ch-'A'] < 0)flag = 1;}}if (flag == 0){for (i = 0; i < 10+26+26; i++)extra += shop_str[i];printf("Yes %d\n", extra);}else{for (i = 0; i < 10+26+26; i++)if (shop_str[i] < 0)miss += shop_str[i];printf("No %d\n", (-1)*miss);}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421194.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.net动态控件的使用(listview ,treeview,tabControl)

对于控件中显示的数据可能是不固定的。如果固定&#xff0c;数据发生了变化&#xff0c;又要重新设计&#xff0c;这样浪费成本&#xff0c;资源。 所以有用代码操作控件&#xff0c;自由度&#xff0c;可控度高。 1&#xff0c;树控件 //把 tv 作为全局变量 &#xff0c; 这是…

泡茶看数据结构-临时(对象设计技术)

一.开场白 第二次《DATA STRUCTURES AND PROBLEM SOVLING WITH C》英文授课&#xff0c;让我产生英文写文章的念头。但是&#xff0c;慢慢开始参合英文吧。一下子写的话&#xff0c;怕写出来博客园首页都不敢收录了。^_^&#xff01;今天&#xff0c;从课堂和自己下午看总结下,…

前端学习(1002):简洁版滑动下拉菜单问题解决

快速滑动 不停切换 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><scrip…

js bom and dom

以下的代码只是一些小的例子。我画了一张图来总结js大的结构 <!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title><script>//Point 1 delayer and timer (BOM browser Object Model)var delayer;var tim…

leetcode 383 赎金信 C++

自己想的&#xff0c;一个思路两个解法&#xff0c;从字符串中的第一个唯一字符的思路搬过来的 one class Solution { public:bool canConstruct(string ransomNote, string magazine) {int table2[26]{0};for(int i0;i!magazine.length();i){table2[magazine[i]-a];}for(int …

Win32下 Qt与Lua交互使用(二):在Lua脚本中使用Qt类

话接上篇。成功配置好QtLuatoLua后&#xff0c;我们可以实现在Lua脚本中使用各个Qt的类。直接看代码吧。 #include "include/lua.hpp" #include <QWidget> #include <QApplication> #include <QFile> #include <QDebug>static int tolua_new…

1099. Build A Binary Search Tree (30)

1099. Build A Binary Search Tree (30) 时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains o…

mysql列属性auto(mysql笔记四)

常见的的是一个字段不为null存在默认值 没值得时候才去找默认值&#xff0c;可以插入一个null到 可以为null的行里 主键&#xff1a;可以唯一标识某条记录的字段或者字段的集合 主键设置 主键不可为null,声明时自动设置为not null 字段上设置 字段名 primary key定义完字段后 …

详解html结构之间的各个关系,层级关系(以列表为例)

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>层级关系查找元素</title></head><body><div id "div">hello<ul id ""><li>li1</li><li>li2</…

css和 js 改变html里面的定位。

css和 js 改变html里面的定位。&#xff08;三种方式&#xff09; <style type"text/css">#div1{border: 1px aquamarine solid;/*固定定位&#xff1a;此元素在整个网页的位置不变,固定某处不动*/position : fixed;left: 20px;top: 10px;}#div2{/*相对定位&am…

unity3d由多个部分组成一个角色

摘自http://forum.unity3d.com/threads/16485-quot-stitch-multiple-body-parts-into-one-character-quot So I have many many models. Each has a skeleton, material, etc. I want to take some of these and combine them into one so I can apply animation commands to …

解决ListView 缓存机制带来的显示不正常问题

ListView加载数据原理:系统绘制ListView时&#xff0c;首先会用getCount&#xff08;&#xff09;函数得到要绘制的这个列表的长度&#xff0c;然后开始逐行绘制。然后调用getView()函数&#xff0c;在这个函数里面首先获得一个View&#xff08;简单item&#xff0c;如字符串或…

windows phone (12) 小试自定义样式

windows phone (12) 小试自定义样式 原文:windows phone (12) 小试自定义样式样式在BS开发中经常用到&#xff0c;在wp中系统也提供了解决办法&#xff0c;就是对设置的样式的一种资源共享&#xff0c;首先是共享资源的位置&#xff0c;它是在App类中&#xff0c;之前我们已经有…

xdoj判断堆栈出栈序列是否有效c++

我在leetcode上写过类似的题&#xff0c;这个代码在xdoj上只能得***50***分&#xff0c;跪求各位大佬挑挑毛病。 #include<stack> #include<iostream> #include<vector> using namespace std; int main(){vector<int>poped;stack<int>s;int n,t;…

ArrayList的remove方法(重写equals方法) 与LinkedList的常用操作

package C12_18;import java.util.ArrayList;public class joy {public static void main(String[] args) {show();//重写equals方法&#xff0c;移除集合里面的元素。public static void show() {ArrayList<dog> al new ArrayList<dog>();al.add(new dog("j…

android学习日记13--数据存储之ContentProvide

3、ContentProvider  数据在Android当中是私有的&#xff0c;当然这些数据包括文件数据和数据库数据以及一些其他类型的数据。ContentProvider实现多应用程序间的数据共享类一般利用ContentProvider为需要共享的数据定义一个URI(统一资源定位符)然后其他程序通过Context获得C…

cin,cin.get(),getline()

我势必扫清我对c的各种疑惑&#xff0c;重拾csdn水文之任 结论&#xff1a;cin在获得需要接受的东西之前&#xff0c;对缓冲区里的空格和换行符不会理睬(但是会把它们从缓冲区删去)&#xff0c;但如果达到了可以结束接受的时候&#xff0c;空格和换行符都会让cin不再接 收,并且…

Java 字节和字符流的读写+Buffered

一个关于IO流的导图 IO流字节的读写&#xff0c;实现复制 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class TestCopy {public static void main(String[] args) throws IOException {copyT…

1070. 结绳(25)

1070. 结绳(25) 时间限制200 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue给定一段一段的绳子&#xff0c;你需要把它们串成一条绳。每次串连的时候&#xff0c;是把两段绳子对折&#xff0c;再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳…