首页 > 试题广场 >

乒乓球筐

[编程题]乒乓球筐
  • 热度指数:3784 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
nowcoder有两盒(A、B)乒乓球,有红双喜的、有亚力亚的……现在他需要判别A盒是否包含了B盒中所有的种类,并且每种球的数量不少于B盒中的数量,该怎么办呢?

输入描述:
输入有多组数据。
每组数据包含两个字符串A、B,代表A盒与B盒中的乒乓球,每个乒乓球用一个大写字母表示,即相同类型的乒乓球为相同的大写字母。
字符串长度不大于10000。


输出描述:
每一组输入对应一行输出:如果B盒中所有球的类型在A中都有,并且每种球的数量都不大于A,则输出“Yes”;否则输出“No”。
示例1

输入

ABCDFYE CDE<br/>ABCDGEAS CDECDE

输出

Yes<br/>No

python solution,使用Counter或者defaultdict,都非常简单:

from collections import Counter

while True:
    try:
        a, b = input().split()
        res = True
        for i in Counter(b):
            if Counter(a)[i] - Counter(b)[i] < 0:
                res = False
                break
        print("Yes" if res else "No")
    except:
        break
编辑于 2017-10-11 10:00:53 回复(0)
import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		while (in.hasNext())
		{
			boolean contain = true;
			StringBuffer input = new StringBuffer(in.next());
			char[] find = in.next().toCharArray();
			for (char c : find)
			{
				int index = input.indexOf(String.valueOf(c));
				if (index != -1)
					input.deleteCharAt(index);
				else
				{
					System.out.println("No");
					contain = false;
					break;
				}
			}
			if (contain)
				System.out.println("Yes");
		}
	}
}

发表于 2020-02-22 16:44:45 回复(0)
public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNextLine()) {
            String r = read.nextLine();
            String[] boxs = r.split(" ");
            ArrayList<Character> A = new ArrayList<Character>();
            ArrayList<Character> B = new ArrayList<Character>();
            for(int i = 0; i < boxs[0].length(); i++) {
                A.add(boxs[0].charAt(i));
            }
            for(int i = 0; i < boxs[1].length(); i++) {
                B.add(boxs[1].charAt(i));
            }
            for(int i = 0; i < B.size(); i++) {
                if(A.contains(B.get(i))) {
                    A.remove(A.get(A.indexOf(B.get(i))));
                    B.remove(i);
                    i--;
                }
            }
            if(B.size() == 0) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
            A = null;
            B = null;
        }
    }

发表于 2018-04-03 22:40:35 回复(0)
#include<stdio.h>
#include<string.h>
#define N 10001
int main()
{
	int a[26],b[26];
	char A[N],B[N];
	while(scanf("%s %s",A,B)!=-1)
	{
		int i,j,flag=1;
		int len_A=strlen(A);
		int len_B=strlen(B);
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(i=0;i<len_A;i++)
		{
			j=A[i]-'A';
			a[j]++;
		}
		for(i=0;i<len_B;i++)
		{
			j=B[i]-'A';
			b[j]++;
		}

		for(i=0;i<26;i++)
		{
			//printf("%d %d\n",a[i],b[i]);
			if((a[i]==0&&b[i]>0)||b[i]>a[i])
			{
				flag=0;
				break;
			}
		}
		if(flag)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}

编辑于 2020-04-14 11:29:05 回复(0)
思路: map的应用。
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
    string A, B;
    while(cin >> A >> B)
    {
        map<char, int> AA, BB;
        for (int i = 0; i < A.size(); i++)
        {
            AA[A[i]]++;
        }
        for (int i = 0; i < B.size(); i++)
        {
            BB[B[i]]++;
        }
        for (int i = 'A'; i <= 'Z'; i++)
        {
            if (BB[i] > AA[i])
            {
                cout << "No" << endl;
                break;
            }
            if (i == 'Z')
            {
                cout << "Yes" << endl;
            }
        }

    }
}

发表于 2018-08-13 14:29:05 回复(0)
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
	string str, res;
	while (cin >> str >> res)
	{
		bool sta = true;
		map<char, int> strdata, resdata;
		for (auto c : str)
			strdata[c]++;
		for (auto c : res)
			resdata[c]++;
		for (map<char, int>::iterator it = resdata.begin(); it != resdata.end(); it++)
			if (strdata[it->first] < it->second)
			{
				sta = false;
				break;
			}
		if (true == sta)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	return 0;
}

发表于 2017-04-22 20:17:59 回复(0)
whileTrue:
    try:
        A, B = raw_input().split()
        iflen(A) < len(B):
            print "No"
            continue
        alpha = [0] * 26
        fori in A:
            alpha[ord(i) - 65] += 1
        flag = True
        fori in B:
            alpha[ord(i) - 65] -= 1
            ifalpha[ord(i) - 65] < 0:
                flag = False
                break
        print "Yes"ifflag else"No"
    except:
        break

发表于 2016-06-28 11:43:20 回复(0)
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(){
    string a, b;
    while(cin >> a >> b){
        map<char, int> ma, mb;
        for(auto e : a){
            ++ma[e];
        }
        for(auto e : b){
            ++mb[e];
        }
        bool flag = true;
        //for(map<char, int>::iterator it = mb.begin(); it != mb.end(); ++it){
            //if(ma[it->first] < it->second){
        for(auto e : mb){
            if(ma[e.first] < e.second){
                flag = !flag;
                break;
            }
        }
        if(flag){
            cout << "Yes" << endl;
        }
        else{
            cout << "No" << endl;
        }
    }
    return 0;
}

发表于 2020-09-06 10:14:25 回复(1)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;

int main() {
    string str1, str2;
    while (cin >> str1 >> str2) {
        bool flag = true;
        unordered_map<char, int> mp1, mp2;
        for (auto& e : str1) mp1[e]++;
        for (auto& e : str2) mp2[e]++;

        for (auto& e : mp2) {
            auto it = mp1.find(e.first);
            if (it != mp1.end()) {
                if (e.second > mp1[e.first]) {
                    flag = false;
                }
            } else {
                flag = false;
            }
        }
        if(flag == true)
            cout << "Yes" << endl;
        else 
            cout << "No" << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

发表于 2023-07-29 18:22:18 回复(0)
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    private static boolean basket(String str1, String str2) {
        if (str1.length() == 0) {
            return false;
        }
        if (str2.length() == 0) {
            return true;
        }
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < str1.length(); i++) {
            if (map.containsKey(str1.charAt(i))) {
                map.put(str1.charAt(i), map.get(str1.charAt(i)) + 1);
            } else {
                map.put(str1.charAt(i), 1);
            }
        }
        for (int i = 0; i < str2.length(); i++) {
            if (map.containsKey(str2.charAt(i))) {
                map.put(str2.charAt(i), map.get(str2.charAt(i)) - 1);
                if (map.get(str2.charAt(i)) < 0) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
            String[] strings = str.split(" ");
            if (basket(strings[0], strings[1])) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}

发表于 2023-05-17 10:19:24 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            String[] str = scanner.nextLine().split(" ");
            String strA = str[0];
            String strB = str[1];

            //1 遍历 A 保存元素
            int[] arr = new int[26];
            Arrays.fill(arr, 0);
            for(int i = 0; i < strA.length(); i++){
                arr[strA.charAt(i) - 'A']++;
            }

            //2 遍历 B 删除元素,查看是是否含有且大于该元素
            boolean flag = true;
            for(int i = 0; i < strB.length(); i++){
                //删除元素
                arr[strB.charAt(i) - 'A']--;

                //判断元素是小于 0
                if(arr[strB.charAt(i) - 'A'] < 0){
                    flag = !flag;
                    break;
                }
            }

            System.out.println(flag ? "Yes" : "No");
        }
    }
}

发表于 2022-11-16 22:34:26 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String boxA = in.next();
            String boxB = in.next();

            Map<Character,Integer> mapA = new HashMap<>();
            for(int i = 0;i < boxA.length();i++){
                char a = boxA.charAt(i);
                if(mapA.containsKey(a)){
                    mapA.replace(a,mapA.get(a) + 1);
                }else {
                    mapA.put(a,1);
                }
            }

            Map<Character,Integer> mapB = new HashMap<>();
            for(int i = 0;i < boxB.length();i++){
                char a = boxB.charAt(i);
                if(mapB.containsKey(a)){
                    mapB.replace(a,mapB.get(a) + 1);
                }else {
                    mapB.put(a,1);
                }
            }

            boolean flag = true;
            for(int i = 0;i < boxB.length();i++){
                char a = boxB.charAt(i);
                if(!(mapA.containsKey(a) && mapA.get(a) >= mapB.get(a))){
                    flag = false;
                }
            }
            System.out.println(flag == true ? "Yes" : "No");
        }
    }
}

发表于 2022-07-07 10:16:59 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String a = sc.next();
            String b = sc.next();
            char[] aa = a.toCharArray();
            char[] bb = b.toCharArray();
            Map<Character,Integer> mapA = new HashMap<>();
            Map<Character,Integer> mapB = new HashMap<>();
            for(char e : aa){
                if(!mapA.containsKey(e)){
                    mapA.put(e,1);
                }else{
                    mapA.put(e,mapA.get(e) + 1);
                }
            }
            
            for(char e : bb){
                if(!mapB.containsKey(e)){
                    mapB.put(e,1);
                }else{
                    mapB.put(e,mapB.get(e) + 1);
                }
            }
            
            boolean flg = true;
            for(char e : bb){
                 if(!mapA.containsKey(e) || mapA.get(e) < mapB.get(e)){
                     flg = false;
                     break;
                 }
            }
            if(flg){
                System.out.println("Yes");
            }else{
                 System.out.println("No");
            }
        }
    }
}

编辑于 2022-06-15 16:53:53 回复(0)
import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            String str1=in.next();
            String str2=in.next();
            boolean ret=find(str1,str2);
            if(ret){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
    public static boolean find(String str1,String str2){
        
        int[]arr=new int[26];
        //定义容量为26的数组存储数字
        //遍历字符串1,存储对应的字母数量 ,比如A对应的就是数组0号元素
        for(int i=0;i<str1.length();i++){
            char ch=str1.charAt(i);
            int temp=(int)(ch-65);
            arr[temp]++;
        }
        //遍历字符串2,找到对应字母下标,进行相减
        for(int i=0;i<str2.length();i++){
            char ch=str2.charAt(i);
            int temp=(int)(ch-65);
            //对应下标容量为0,说明A盒子没有,或者A盒子里的数量小于B盒子的数量
            //返回false;
            if(arr[temp]==0){
                return false;
            }
            arr[temp]--;
        }
        //结束字符串2的遍历,没有返回false。说明满足条件
        return true;
    } 
}
发表于 2022-03-10 17:22:54 回复(0)
import java.util.*;

public class Main{
    public static String IsOKContais(String str){
        String[] ret = str.split(" ");
        Map<Character,Integer> map = new HashMap<>();//A
        Map<Character,Integer> map0 = new HashMap<>();//B
        for(int i = 0;i < ret[0].length();i++){
            if(!map.containsKey(ret[0].charAt(i))){
                map.put(ret[0].charAt(i),1);
            }else{
                map.put(ret[0].charAt(i),map.get(ret[0].charAt(i)) + 1);
            }
        }
        for(int i = 0;i < ret[1].length();i++){
            if(!map0.containsKey(ret[1].charAt(i))){
                map0.put(ret[1].charAt(i),1);
            }else{
                map0.put(ret[1].charAt(i),map0.get(ret[1].charAt(i)) + 1);
            }
        }
        
        for(Map.Entry<Character,Integer> entry:map0.entrySet()){
            Character Key = entry.getKey();
            Integer Value = entry.getValue();
            if(map.containsKey(Key)){
                if(map.get(Key) < Value){
                    return "No";//包含但数目不对
                }
            }else{
                return "No";//不包含
            }
        }
        return "Yes";
    }
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            String str = scan.nextLine();
            System.out.println(IsOKContais(str));
        }
    }
}

发表于 2021-08-03 20:32:30 回复(0)
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            String[] tem = str.split(" ");
            HashMap<Character, Integer> A = new HashMap<>();
            HashMap<Character, Integer> B = new HashMap<>();
            boolean res = true;
            for (int i = 0; i < tem[0].length(); i++) {
                if (!A.containsKey(tem[0].charAt(i))) {
                    A.put(tem[0].charAt(i), 1);
                } else {
                    A.put(tem[0].charAt(i), A.get(tem[0].charAt(i)) + 1);
                }
            }
            for (int i = 0; i < tem[1].length(); i++) {
                if (!B.containsKey(tem[1].charAt(i))) {
                    B.put(tem[1].charAt(i), 1);
                } else {
                    B.put(tem[1].charAt(i), B.get(tem[1].charAt(i)) + 1);
                }
            }
            Iterator<Character> iterator = B.keySet().iterator();
            while (iterator.hasNext()) {
                Character key = iterator.next();
                if (A.containsKey(key) && A.get(key) >= B.get(key)) {
                    continue;
                }else {
                    res = false;
                    break;
                }
            }
            if (res) System.out.println("Yes");
            if (!res) System.out.println("No");
        }
    }
}

发表于 2021-06-09 22:08:15 回复(0)
好奇怪,我在cb上的结果都是正确的,提交却不过不了,有大佬知道我错在哪吗?
#include <bits/stdc++.h>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    string s1,s2;
    int table[27];
    memset(table,0,sizeof(table));
    while(cin>>s1>>s2)
    {
        int len1=s1.size();
        int len2=s2.size();
        for(int i=0; i<len1; i++)
        {
            table[s1[i]-'A']++;
        }
        int flag=1;
        for(int i=0; i<len2; i++)
        {
                if(--table[s2[i]-'A']<0)
                {
                    flag=0;
                    break;
                }
        }
        if(flag)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}


发表于 2020-08-23 16:55:28 回复(2)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            Map<Character,Integer>map=new HashMap<>();
            String[]str=new String[2];
            str=sc.nextLine().split(" ");
            /*System.out.println(str[0]);
            System.out.println(str[1]);*/
            String searh = searh(str, map);
            System.out.println(searh);
        }

    }

    private static String searh(String[] str,Map<Character,Integer> map) {
        for (int i = 0; i <str[0].length() ; i++) {
            if (map.containsKey(str[0].charAt(i))){
                map.put(str[0].charAt(i),map.get(str[0].charAt(i))+1);
            }else {
                map.put(str[0].charAt(i),1);
            }
        }
        for (int i = 0; i <str[1].length() ; i++) {
            if (map.containsKey(str[1].charAt(i))){
                map.put(str[1].charAt(i),map.get(str[1].charAt(i))-1);
            }else {
                return "No";
            }
        }
        for (int i = 0; i <str[0].length() ; i++) {

            if (map.get(str[0].charAt(i))<0){
              //  System.out.println(map.get(str[0].charAt(i)));
                return "No";
            }
        }
        return "Yes";
    }
}
笨比解法 我摊牌了 我是笨比😥
发表于 2020-07-22 17:58:53 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            char[] A = in.next().toCharArray();
            char[] B = in.next().toCharArray();
            if(just(A,B)){
                System.out.println("Yes");
            }else {
                System.out.println("No");
            }
        }
    }

    private static boolean just(char[] a, char[] b) {
       int count=0;
       int flag =b.length;
        for (int i = 0; i <a.length ; i++) {
            for (int j = 0; j < b.length; j++) {
                if(b[j]==a[i]){
                    count++;
                    b[j]='\0';
                    break;
                }
            }
        }
        if(count==flag){
            return true;
        }else {
            return false;
        }
    }
}

终于好了,老是数组越界 吐了!!!
发表于 2020-07-21 23:43:47 回复(0)
#include<iostream>
#include<vector>
using namespace std;

int main()
{
    string A;
    string B;
    while(cin>>A>>B)
    {
        int i;
        int flag=1;
        vector<int>numA(200,0);//每次输入都要重新定义一下
        for(i=0;i<A.size();i++)
        {
            numA[A[i]]++;//计数增加
        }
        for(i=0;i<B.size();i++)
        {
            numA[B[i]]--;//计数减小
            if(numA[B[i]]<0)
            {
                flag=0;
                break;
            }
        }
        if(flag)
         cout<<"Yes"<<endl;
        else
         cout<<"No"<<endl;
    }
    return 0;
}
发表于 2020-06-05 22:43:15 回复(0)