PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]
题目
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.
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 2:
No 2
题目分析
字符串a,b
- b中字符出现次数<=其在a中出现次数,输出Yes a中多余字符出现次数
- b中字符出现次数>其在a中出现次数,输出No a中缺少字符数
解题思路
算法1
- 统计a中字符出现的次数,记录在asc数组中
- 使用df记录缺少字符数
- 遍历b中字符,当前字符为b[i]
- 若asc[b[i]]大于0,减一
- 若asc[b[i]]等于0,df++(缺少数+1)
- 判断df值,并打印
- 若df==0,表明不缺少字符,输出a中多余字符--a的长度-b的长度
- 若df!=0,表明缺少字符,输出df
算法2
- 统计a,b中字符出现次数,记录在容器asc1,asc2中
- 使用df记录缺少字符数
- 遍历b
- 若asc1[b[i]]<asc2[b[i]],df++(缺少数+1);
- 若asc2[b[i]]>=asc2[b[i]],不缺少,跳过
- 判断df值,并打印
- 若df==0,表明不缺少字符,输出a中多余字符--a的长度-b的长度
- 若df!=0,表明缺少字符,输出df
Code
Code 01(算法1、最优)
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char * argv[]){
string a,b;
cin>>a>>b;
int asc[256]={0};
for(int i=0;i<a.length();i++){
asc[a[i]]++;
}
int df=0;
for(int i=0;i<b.length();i++){
if(asc[b[i]]>0)asc[b[i]]--;
else df++;
}
if(df==0)printf("Yes %d",a.length()-b.length());
if(df!=0)printf("No %d",df);
return 0;
}
Code 02(算法2、int array)
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char * argv[]) {
char s1[1001];
char s2[1001];
cin.getline(s1,1001);
cin.getline(s2,1001);
int len1=strlen(s1),len2=strlen(s2);
int asc1[256]= {0};
int asc2[256]= {0};
for(int i=0; i<len1; i++) asc1[s1[i]]++;
for(int i=0; i<len2; i++) asc2[s2[i]]++;
int df=0;
int ascp[256]= {0};
for(int i=0; i<len2; i++) {
if(ascp[s2[i]]==0&&asc2[s2[i]]>asc1[s2[i]]) {
df+=asc2[s2[i]]-asc1[s2[i]];
ascp[s2[i]]=1;
}
}
if(df==0)printf("Yes %d",len1-len2);
if(df!=0)printf("No %d",df);
return 0;
}
Code 03(算法2、map)
#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
int main(int argc, char * argv[]) {
char s1[1001];
char s2[1001];
cin.getline(s1,1001);
cin.getline(s2,1001);
int len1=strlen(s1),len2=strlen(s2);
unordered_map<char,int> m1,m2;
for(int i=0; i<len1; i++) m1[s1[i]]++;
for(int i=0; i<len2; i++) m2[s2[i]]++;
int df=0;
int ascp[256]= {0};
for(int i=0; i<len2; i++) {
if(ascp[s2[i]]==0&&m2[s2[i]]>m1[s2[i]]) {
df+=m2[s2[i]]-m1[s2[i]];
ascp[s2[i]]=1;
}
}
if(df==0)printf("Yes %d",len1-len2);
if(df!=0)printf("No %d",df);
return 0;
}
PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]的更多相关文章
- PAT Advanced 1084 Broken Keyboard (20) [Hash散列]
题目 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the charact ...
- PAT Advanced 1050 String Subtraction (20) [Hash散列]
题目 Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer taking all th ...
- PAT Advanced 1041 Be Unique (20) [Hash散列]
题目 Being unique is so important to people on Mars that even their lottery is designed in a unique wa ...
- PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT Basic 1047 编程团体赛(20) [Hash散列]
题目 编程团体赛的规则为:每个参赛队由若⼲队员组成:所有队员独⽴⽐赛:参赛队的成绩为所有队员的成绩和:成绩最⾼的队获胜.现给定所有队员的⽐赛成绩,请你编写程序找出冠军队. 输⼊格式: 输⼊第⼀⾏给出⼀ ...
- PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...
- PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...
- PAT (Advanced Level) 1069. The Black Hole of Numbers (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT (Advanced Level) 1065. A+B and C (64bit) (20)
因为会溢出,因此判断条件需要转化.变成b>c-a #include<cstdio> #include<cstring> #include<cmath> #in ...
随机推荐
- 四十四、在SAP中冻结第一行表头
一.表格数据量大了,如果需要界面滚动,则看不到第一行的表头文本 二.代码如下: 二.效果如下,任意滚动,表头还是被冻结可以看到
- 146-PHP 使用<<<和HTML混编(二)
<?php $html=<<<HTM1 <title>PHP输出HTML代码</title> <body> <a href=#> ...
- Java中的String介绍
一.概述 String是代表字符串的类,本身是一个最终类,使用final修饰,不能被继承. 二.String字符串的特征 1. 字符串在内存中是以字符数组的形式来存储的. 示例如下,可以从String ...
- (转)null和NULL和nullptr和””区别
突然想到这个有趣的问题:C语言和C++对大小写是敏感的,也就是说null和NULL是区别对待的.NULL代表空地址,null只是一个符号.便来深究,看了很多资料,总结如下: 其实null和NULL都是 ...
- Q3狂揽3亿美元净利润的特斯拉会让国内电动汽车厂商喜极而泣吗?
作为电动汽车行业的标杆,特斯拉无疑是国内电动汽车厂商发展进程中重要的参考对象.而前段时间特斯拉身上出现的产能受阻.私有化风波.马斯克卸任董事长一职等事件,着实让国产电动汽车厂商惊出一身冷汗.毕竟如果特 ...
- C++编程学习(一) 概述
从一年前开通博客,陆陆续续写了一些总结类的东西,但是一直没push到网上.现在发现,分享自己的笔记也是自身学习.与他人交流的好方式.从今天开始,我会经常的push一些学习笔记到博客中,先从C++开始吧 ...
- 3 react 简书 添加 头部搜索动态效果
1. 添加动态效果组件 yarn add react-transition-group 2. 修改 src/common/header/index.js import React, {Componen ...
- c# 属性 (get、set)
//属性是一种用于访问对象或类的特性的成员.属性可以包括字符串的长度.字体的大小.窗体的标题和客户的名称.属性是成员的自然扩展,二者都是关联类型的命名成员.namespace ConsoleAppli ...
- VUE注册全局组件和局部组件
全局组件 第一步:在components文件夹下建立一个子文件Users.vue <template> <div class="users"> {{msg} ...
- 尝试用kotlin做一个app(二)
导航条 我想实现的效果是这样的 类似于ViewPager的效果,子类导航页面可以滑动,当滑动某个子类导航页面,导航线会平滑地向父类导航移动 ·添加布局 <!--导航分类:编程语言/技术文档/源码 ...