牛客网问题
牛客网94题候选人投票问题 题干描述:输入候选人的人数n,第二行输入n个候选人的名字(均为大写字母的字符串),第三行输入投票人的人数,第四行输入投票。 输入: 4 A B C D 8 A D E CF A GG A B 输出: A : 3 B : 1 C : 0 D : 1 Invalid : 3
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
int n = Integer.parseInt(in.nextLine());
String a = in.nextLine();
int p = Integer.parseInt(in.nextLine());
String b = in.nextLine();
int count = 0;
Map<String,Integer> map = new LinkedHashMap<>();
for(String str:a.split(" ")){
map.put(str,0);
}
for(String str:b.split(" ")){
if(map.containsKey(str)){
map.put(str,map.get(str)+1);
count++;
}
}
for(String key:map.keySet()){
System.out.println(key+" : "+map.get(key));
}
System.out.println("Invalid : "+(p-count));
}
}
}