1092 To Buy or Not to Buy (字符串删除)

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:

ppRYYGrrYBR2258
YrR8RrY

Sample Output 1:

Yes 8

Sample Input 2:

ppRYYGrrYB225
YrR8RrY

Sample Output 2:

No 2思路:  这个题目算是目前遇到的最简单的了。我的办法是遍历eva的字符串,对于该字符串中的每一个字符,在店主的字符串中查找,如果没有找到,则缺少一个字符,如果找到了,则把在店主的字符串中去除这个字符。最后如果lack变量大于0,则输出No,否则输出店主字符串的长度即可。这样做的确可以通过,并且不会超时,看了一下别人的解法,感觉其他人的使用哈希或者dfs都很复杂。
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
using namespace std;

int main()
{
    string owner,eva;
    cin>>owner>>eva;
    int lack=0;
    for(int i=0;i<eva.size();i++)
    {
        size_t index=owner.find(eva[i]);
        if(index!=string::npos)
            owner.erase(index,1);
        else
            lack++;
    }
    if(lack>0)
        cout<<"No "<<lack;
    else
        cout<<"Yes "<<owner.size();
    return 0;
}
 

原文地址:https://www.cnblogs.com/zhanghaijie/p/10303285.html

时间: 2024-10-10 07:33:03

1092 To Buy or Not to Buy (字符串删除)的相关文章

【PAT甲级】1092 To Buy or Not to Buy (20分):哈希

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805374509498368 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

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 b

1092. To Buy or Not to Buy (20)【水题】——PAT (Advanced Level) Practise

题目信息 1092. To Buy or Not to Buy (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 o

pat 1092 To Buy or Not to Buy(20 分)

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 w

poj1092. 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 b

字符串删除问题

在计算机的世界里,字符串问题可以说是一个很重要的问题,比如文本处理等等问题.今天Mayuyu就来讲述一个字符串删除问题,问题描述如下 问题:给定一个很长的字符串,比如长度为1000000,现在要删除这个字符串中某些指定的字符,这些指定的字符只 有几个,现在Mayuyu要求是尽量用最少的时间和空间来做这件事. 分析:很明显,可以从前往后扫描,遇到一个指定的字符就删除,然后把后面的往前移动.很显然,这样做时间复杂度 太大啊,所以很有必要设计一个比较好的算法.其实可以这样考虑,我们可以用两个指针从前往

字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)

1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const char *pStrSource) { assert(NULL!=pStrDest && NULL!=pStrSource); char *strTemp=pStrDest; while ((*pStrDest++ = *pStrSource++) != '\0'); return strTemp

python字符串删除,列表删除以及字典删除的总结

一:字符串删除  1,字符串本身是不可变的,一个字符串定义以后,对他本身是不能做任何操作的,所以的增删改都是对原字符串拷贝的副本的操作,原来的字符串还是原来的字符串,它本身并没 有变 2,字符串本身是不能修改的,但是可以通过其他方法来达到一个看似修改的效果,比如,切片+拼接   replace()替换,strip(), rstrip(),lstrip()去掉两端的字符 二:列表删除 1.pop(index) 2.remove(元素) 3.del 切片 4.clear()清空列表 三:字典删除 1

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 c