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 ...
随机推荐
- Power Tower
题目大意:给出一段长为 \(n\) 的序列 \(a_1,a_2,\cdots,a_n\) ,一个模数 \(m\) .每次询问给定 \(l,r\) 求 \(a_l^{{a_{l+1}^\cdots}^{ ...
- 【Android】家庭记账本手机版开发报告六
一.说在前面 昨天 1.创建登入和注册界面:2.向数据库添加一张用户表 今天 用图标显示账单情况 问题 1.使用第三方库 hellochart,时添加依赖构建失败 2.在 chertFragmen ...
- SpringBoot+SpringSecurity之如何forword到登录页面
当我们在项目中引入了SpringSecurity框架进行身份校验的时候,如果某个请求需要用户身份认证,那么SpringSecurity会将用户redirect到登录页面.但是有些时候我们希望是forw ...
- 电脑使用热键时是否需按住Fn键相关说明
ThinkPad E系列机型 方法一: 在开机出现ThinkPad标志时,连续点F1(若无反应,请尝试Fn+F1)进入BIOS设置. 在BIOS中,依次选择Config---Keyboard/Mous ...
- mac允许“任何来源”下载的应用
刚买的mac电脑,我们不止在App Store上下载,还会通过浏览器下载 有的时候需要下载一些破解的软件,这个时候安装会提示文件被破坏,很是头疼 不用着急,这是因为mac会判断app如果被破坏,就不允 ...
- 剑指offer题目汇总
二维数组中的查找 题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中 ...
- LA_4730 Kingdom 并查集+树状数组
给定N个点的坐标,代表N各城市,有M种操作,共分两种,一种是连线,把两个点连起来(一旦构成连通图,这个连通图即为一个州),还有种询问操作,为y=c,(c为小数部分恒为.5的实数),问y=c这条线经过了 ...
- C语言备忘录——向上取整
众所周知,C语言的取整方式是向下取整,昨天老师留了一道思考题,问我们C语言怎么向上取整,当时我第一反应就是ceil(),老师说不能用if……else之类的,函数也不行.当时想了想没事不用就不用,去ma ...
- XML--XSL
参考 http://blog.51cto.com/cnn237111/1345998 https://www.w3.org/TR/2017/REC-xslt-30-20170608/ 声明 把文档声明 ...
- Java SE 5.0(JDK 1.5)新特性
目录 自动装箱与拆箱 枚举(常用来设计单例模式) 静态导入static import 可变参数(Varargs) 内省(Introspector) 泛型(Generics) For-Each循环 ja ...