题目

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

  1. 统计a中字符出现的次数,记录在asc数组中
  2. 使用df记录缺少字符数
  3. 遍历b中字符,当前字符为b[i]
    • 若asc[b[i]]大于0,减一
    • 若asc[b[i]]等于0,df++(缺少数+1)
  4. 判断df值,并打印
    • 若df==0,表明不缺少字符,输出a中多余字符--a的长度-b的长度
    • 若df!=0,表明缺少字符,输出df

算法2

  1. 统计a,b中字符出现次数,记录在容器asc1,asc2中
  2. 使用df记录缺少字符数
  3. 遍历b
    • 若asc1[b[i]]<asc2[b[i]],df++(缺少数+1);
    • 若asc2[b[i]]>=asc2[b[i]],不缺少,跳过
  4. 判断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;
}

最新文章

  1. [题解]USACO 1.3 Wormholes
  2. 用Dart写的黑白棋游戏
  3. Android开源项目
  4. 如何让网页在浏览器标题栏显示自己制作的图标ico
  5. webpack学习笔记一
  6. 情定XMLA,割舍不下的XAML
  7. javascript console 函数详解 js开发调试的利器
  8. Notepad++ install vi plugin
  9. hdu2015
  10. MySQL类型属性Unsigned与ZeroFill
  11. PHP5新语法学习
  12. ORM(三)QuerySet查询字段操作
  13. 软件测试:lab1.Junit and Eclemma
  14. jquery---筛选总结
  15. linux内存黑洞
  16. Shell脚本-自动化部署WEB
  17. VMware 中安装虚拟机和宿主机通信
  18. MyCat(一) - 初体验
  19. Loadrunner加密算法脚本与token作为get请求url上的参数处理
  20. Android开发 ---xml构建选项菜单、上下文菜单(长按显示菜单)、发通知、发送下载通知

热门文章

  1. StackExchange.Redis.DLL 操作redis加强版
  2. Vim中的基本操作
  3. hadoop的文件操作整理java
  4. Codeforces 392 C Unfair Poll(模拟)
  5. 使用GDI+显示OpenCV中的图像IplImage
  6. 如何生成 SSH keys, 并在 Github 或 Gitlab 等上添加密钥
  7. SQL的7种连接查询详细实例讲解
  8. C++基础--引用的一点补充
  9. Spring Boot2(001):入门介绍和一些官网链接参考
  10. 对DataFrame的再理解