涂鸦问题(度小满笔试)

题目描述:

小A正在学画画,现在,线稿已经画好了,只剩下涂色部分了。但是小A发现,他的颜料不够了。每一块颜料能涂一个色块,
每一个色块的颜色是事先决定好了的。 由于颜料不够,小A只能尽其所能来涂色。
如果一个色块没有了颜料,就不能涂色。现在,给你画中需要的色块颜色,和小A现在手上有的颜料,请你计算小A能涂多少个色块。
 
输入描述
输入包含两个字符串,都仅包含大写字母,每一种字母代表一种颜色。 第一个字符串S代表小A手上的颜料,第二个字符串T代表画需要的颜料。
 
1≤|S|,|T|≤1000
 
输出描述
输出包含一个数,即最多能涂多少个色块。
 
样例输入
AAB
ABC
样例输出
2
提示

小A拥有两个A颜料,一个B颜料,用了一个A颜料一个B颜料,总共涂了两个色块。
 1 import java.util.HashMap;
 2 import java.util.Map;
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         Scanner input = new Scanner(System.in);
 8         String str = input.nextLine();
 9         String obj = input.nextLine();
10         Map<Character, Integer> strMap = new HashMap<Character, Integer>();
11         Map<Character, Integer> objMap = new HashMap<Character, Integer>();
12 
13         for (int i = 0; i < str.length(); i++) {
14             char c = str.charAt(i);
15             if (strMap.containsKey(c)) {
16                 int num = strMap.get(c);
17                 strMap.put(c, ++num);
18             } else {
19                 strMap.put(c, 1);
20             }
21         }
22 
23         for (int i = 0; i < obj.length(); i++) {
24             char c = obj.charAt(i);
25             if (objMap.containsKey(c)) {
26                 int num = objMap.get(c);
27                 objMap.put(c, ++num);
28             } else {
29                 objMap.put(c, 1);
30             }
31         }
32         int total = 0;
33         for (int i = 0; i < str.length(); i++) {
34             char c = str.charAt(i);
35             int pre = strMap.get(c);
36             if (objMap.containsKey(c)){
37                 int next = objMap.get(c);
38                 if (pre == next) {
39                     total += next;
40 
41                 } else if (pre > next) {
42                     total += next;
43 
44                 } else {
45                     total += pre;
46                 }
47                 strMap.put(c, 0);
48                 objMap.put(c, 0);
49             }
50 
51         }
52         System.out.println(total);
53     }
54 }

 

posted @ 2020-09-21 15:05  王余阳  阅读(358)  评论(0)    收藏  举报