首页 > 试题广场 >

到底买不买(20)

[编程题]到底买不买(20)
  • 热度指数:10925 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一
下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。

为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以买,因为包含了
全部她想要的珠子,还多了8颗不需要的珠子;ppRYYGrrYB225不能买,因为没有黑色珠子,并且少了一颗红色的珠子。

输入描述:
每个输入包含1个测试用例。每个测试用例分别在2行中先后给出摊主的珠串和小红想做的珠串,两串都不超过1000个珠子。


输出描述:
如果可以买,则在一行中输出“Yes”以及有多少多余的珠子;如果不可以买,则在一行中输出“No”以及缺了多少珠子。其间以1个空格分隔。
示例1

输入

ppRYYGrrYBR2258<br/>YrR8RrY

输出

Yes 8

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()){
            String s = in.nextLine();
            String b = in.nextLine();
            StringBuffer sb = new StringBuffer(s);
            String c = "";
            for (int i = 0; i < b.length(); i++) {
                for (int j = 0; j < sb.length() ; j++) {
                    if (b.charAt(i) == sb.charAt(j)) {
                        sb.deleteCharAt(j);
                        c += b.charAt(i);
                        break;
                    }
                }
            }
            if(c.length() == b.length()){
                System.out.println("yes"+" "+ sb.length());
            }else {
                System.out.println("No"+" "+(b.length()-c.length()));
            }
        }
    }
}


发表于 2020-06-24 21:09:53 回复(0)
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String seller = bf.readLine();
        String red = bf.readLine();
        judge(seller,red);
    }

    public static void judge(String seller,String red){
        int count;
        int bad = 0;
        boolean flag = true;
        for(int i=0;i<red.length();i++){
            count = seller.indexOf(red.charAt(i)+"");
            if(count == -1){
                flag = false;
                bad++;
            }
            else{
                seller = seller.substring(0,count)+seller.substring(count+1);;
            }
        }
        if(flag){
            System.out.println("Yes "+seller.length());
        }
        else{
            System.out.println("No "+bad);
        }
    }
}
自己觉得思路和程序都还挺简单的,就分享一下
发表于 2020-05-25 21:45:29 回复(0)
  Java实现,已通过,创建两个 HashMap 分别存储珠子对应的数目再用买家需要的珠子数与卖家的做对比。代码如下:
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String sales=in.nextLine();
        String hers=in.nextLine();
        judge(sales,hers);
    }
        public static void judge(String sale,String hers)
        {
            HashMap<Character,Integer> sales=new HashMap<>();
            HashMap<Character,Integer> buy=new HashMap<>();

            char[] saleChar=sale.toCharArray();
            char[] buys=hers.toCharArray();
            for(int i=0;i<saleChar.length;i++)
            {
                if(sales.get(saleChar[i])==null)
                {
                    sales.put(saleChar[i],1);
                }
                else sales.put(saleChar[i],sales.get(saleChar[i])+1);

            }
            for(int i=0;i<buys.length;i++)
            {
                if(buy.get(buys[i])==null)
                {
                    buy.put(buys[i],1);
                }
             else buy.put(buys[i],buy.get(buys[i])+1);
            }
//卖的比买的多了几个
            int less=0;
            int more=0;
            boolean flag=true;
           for(char c:buy.keySet())
            {//卖的少了
                if(sales.get(c)==null) 
                { flag=false;
                    less=buy.get(c)+less;}
                else
                    //卖的多了
                {if(sales.get(c)>buy.get(c))   
              more=more+(sales.get(c)-buy.get(c));
               else
               {flag=false;   
                   less=less+(buy.get(c)-sales.get(c));}
            }}
if(!flag) System.out.println("No"+" "+less);
           else System.out.println("Yes"+" "+more);
        }
}


发表于 2020-02-26 11:23:54 回复(0)
import java.util.*;
public class text{
    public static void main( String[] args){
        Scanner sca = new Scanner(System.in);
        while(sca.hasNext()){
            String Waiter = sca.next();
            String Hong = sca.next();
            int num = 1;
            int len = Waiter.length();
            if(Waiter == null){
                System.out.println("No"+" "+ Hong.length());
            }
            for(int j = 0;j<Hong.length();j++){
                int i = Waiter.indexOf(Hong.substring(j,j+1));
                if(i != -1){
                   Waiter = Waiter.replaceFirst(Waiter.substring(i,i+1),"");
                    num += 1;
                }
            }
            if(Waiter.length()+Hong.length() == len){
                System.out.println("Yes"+" "+num);
            }else{
                System.out.println("No"+" "+
                        String.valueOf(Hong.length()-(len-Waiter.length())));
            }
        }
    }
}

发表于 2019-08-12 00:12:24 回复(0)
 
import java.util.HashMap;
import java.util.Scanner;

public class PAT29 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashMap<Character, Integer> map = new HashMap<>();
        String sample = scanner.next();
        String target = scanner.next();
        int length = sample.length();
        for (int i = 0; i < length; i++) {
            map.merge(sample.charAt(i), 1, Integer::sum);
        }
        int targetLen = target.length();
        int leak = 0;//缺少
        char ch;
        for (int i = 0; i < targetLen; i++) {
            ch = target.charAt(i);
            Integer value = map.get(ch);
            if (value == null || value == 0) {
                leak++;
            }else {
                map.put(ch, value-1);
            }
        }
        if (leak == 0) {
            System.out.println("Yes " + (length - targetLen));
        } else {
            System.out.println("No " + leak);
        }
        scanner.close();
    }
}

发表于 2019-08-09 17:30:10 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            String s=in.next(),t=in.next();
            int lack=0,sl=s.length(),tl=t.length();
            int m1[]=new int[123],m2[]=new int[123];
            for(int i=0;i<sl;i++) 
                m1[(int)s.charAt(i)]++;               
            for(int i=0;i<tl;i++) 
                m2[(int)t.charAt(i)]++;                  
            for(int i=48;i<122;i++) {
                if(m2[i]>m1[i]&&m2[i]!=0)
                    lack+=m2[i]-m1[i];
            }
            System.out.println(lack==0?"Yes "+(sl-tl):"No "+lack);
        }
    }
}

编辑于 2019-07-27 15:57:20 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        //java集合框架
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String have = sc.nextLine();//商家手中的珠串
            String need = sc.nextLine();//用户想要的珠串
            
            //商人
            Map<Character, Integer> h = new HashMap<>();
            for(char c : have.toCharArray()) {
                if(h.containsKey(c)) {
                    h.put(c, h.get(c)+1);
                }else {
                    h.put(c, 1);
                }
            }
            
            //用户
            Map<Character, Integer> n = new HashMap<>();
            for(char c : need.toCharArray()) {
                if(n.containsKey(c)) {
                    n.put(c, n.get(c)+1);
                }else {
                    n.put(c, 1);
                }
            }
            
            //计算差值,以用户为参考
            boolean weatherBy = true;
            int lack = 0;
            for(Map.Entry<Character, Integer> en : n.entrySet()) {
                char k = en.getKey();
                int v = en.getValue();
                if(h.containsKey(k) && h.get(k) < v) {//商人的珠子包含用户的珠子但是数量不够
                    weatherBy = false;
                    lack += v-h.get(k);
                }else if(!h.containsKey(k)) {//商人的珠子不包含用户的珠子
                    weatherBy = false;
                    lack += v;
                }
            }
            
            if(weatherBy) {
                System.out.println("Yes " + (have.length()-need.length()));
            }else {
                System.out.println("No " + lack);
            }
        }
    }
}

发表于 2019-07-05 12:09:02 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String str = sc.next();
        String want = sc.next();

        sc.close();

        int miss = 0;
        for (int i = 0; i < want.length(); i++) {
            if (!str.contains(String.valueOf(want.charAt(i)))) {
                miss++;
            }
            str = str.replaceFirst(String.valueOf(want.charAt(i)), "*");
        }
        if (miss != 0) {
            System.out.print("No " + miss);
        } else {
            System.out.print("Yes " + (str.length() - want.length()));
        }
    }
}
编辑于 2018-12-07 16:11:21 回复(0)
解题思路:
匹配需要的与出售的,每匹配到一个就用空字符串替换一个;如果没匹配到则添加到一个新的空字符串后。
import java.util.Scanner;
public class Main {     public static String judge(String sell, String want) {         String remain = "";         for (int i = 0; i < want.length(); i++) {             if (sell.indexOf(want.charAt(i)) != -1) {                 sell = sell.replaceFirst(want.charAt(i) + "", "");             } else {                 remain += want.charAt(i);             }         }         return remain;     }
    public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         String sell = scanner.next();         String want = scanner.next();         String remain = judge(sell, want);         if (remain.equals("")) {             System.out.println("Yes " + (sell.length() - want.length()));         } else {             System.out.println("No " + remain.length());         }     } }


发表于 2018-09-18 16:05:53 回复(0)
可以AC!!!
import java.util.ArrayList;
import java.util.Scanner;

public class Main {     @SuppressWarnings("resource")     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         String seller = in.next();         String consumer = in.next();         char[] charArraySeller = seller.toCharArray();         char[] charArrayConsumer = consumer.toCharArray();         ArrayList<Object> arrayList = new ArrayList<>();         for (int i = 0; i < charArraySeller.length; i++) {             arrayList.add(String.valueOf(charArraySeller[i]));         }         int loseSum = 0;// 缺失珠子的个数         for (int i = 0; i < charArrayConsumer.length; i++) {             boolean remove = arrayList.remove(String.valueOf(charArrayConsumer[i]));             if (!remove) {                 loseSum++;             }         }         if (loseSum == 0) {             System.out.println("Yes " + String.valueOf(charArraySeller.length - charArrayConsumer.length));         } else {             System.out.println("No " + loseSum);         }     }
}

发表于 2017-09-22 21:23:44 回复(0)
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;


public class Main{
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean weatherBy = true;
		int lack = 0;
		int redundant = 0;
		Scanner sc = new Scanner(System.in);
		String have = sc.nextLine();
		String need = sc.nextLine();
		Map <Character,Integer> h = new HashMap<>();
		Map <Character,Integer> n = new HashMap<>();
		for (char c:have.toCharArray()) {
			if(h.containsKey(c)){
				h.put(c, h.get(c)+1);
			}else{
				h.put(c, 1);
			}
		}
		for (char c : need.toCharArray()) {
			if(n.containsKey(c)){
				n.put(c, n.get(c)+1);
			}else{
				n.put(c, 1);
			}
		}
		Iterator<Entry<Character, Integer>> it = n.entrySet().iterator();
		while(it.hasNext()){
			Entry<Character, Integer> en = it.next();
			char k = en.getKey();
			int v = en.getValue();
			if(h.containsKey(k) && h.get(k)<v){
				weatherBy = false;
				lack += v -h.get(k);
			}else if(! h.containsKey(k)){
				weatherBy = false;
				lack += v;
			}
		}
		if(weatherBy == true){
			System.out.println("Yes "+(have.length()-need.length()));
		}else{
			System.out.println("No "+lack);
		}
	}

}

编辑于 2017-08-20 21:14:55 回复(0)
/**
 *  god,我居然写出来这么漂亮的代码
 *  你们的代码都太过冗余了
 *  我的思路有两个,一个像我这样的,hash散列表
 *  第二个是逐个字符对比,我看楼上的很多消除法,我觉得那可以优化,减少第二层循环次数。
 */

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        byte[] aBytes = sc.nextLine().getBytes();
        byte[] bBytes = sc.nextLine().getBytes();
        short[] map = new short[128];
        short count = 0;

        for (int i = 0, j = Math.max(aBytes.length, bBytes.length); i < j; i++) {
            if (i < aBytes.length) {
                if (map[aBytes[i]] < 0) count++;
                map[aBytes[i]]++;
            }
            if (i < bBytes.length) {
                if (map[bBytes[i]] <= 0) count--;
                map[bBytes[i]]--;
            }
        }

        System.out.printf("%s %d\n", count < 0 ? "No" : "Yes", count < 0 ? -count : aBytes.length - bBytes.length);
    }
}

编辑于 2016-11-13 03:28:56 回复(0)
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            char[] str1 = in.nextLine().toCharArray();
            char[] str2 = in.nextLine().toCharArray();
            boolean[] check =newboolean[str1.length];
            intn =0;
            intnotFind =0;
            for(inti =0; i < str2.length; i++) {
                booleanfind =false;
                for(intj =0; j < str1.length; j++) {
                    if(str2[i] == str1[j] && find ==false&& check[j]==false) {
                        n++;
                        find =true;
                        check[j] =true;
                        break;
                    }
                }
                if(!find) {
                    notFind++;
                }
            }
             
            if(notFind!=0){
                System.out.print("No "+notFind);
            }else{
                System.out.print("Yes "+(str1.length-n));
            }
        }
    }
 
}

编辑于 2016-09-21 16:41:54 回复(0)