第一题
现在你的班级刚刚参加了一个只有单选题的考试。班级一共有n个学生,考试有m个问题。每个题目都有五个可选答案(A,B,C,D,E)。并且每个题目只有一个正确答案。每个题目的分数也不一样,第i个题目的分数用a[i]表示。如果没有正确回答就得0分。
考试结束后,每个学生都记得自己的答案,但是他们还不知道正确答案是什么。如果非常乐观的考虑,他们班级最多可以得到多少分?
输入描述:第一行包含两个整数,n代表代表学生数量,m代表题目数量,下面n行数据包含一个String si,表示第i个学生的答案。si的第j个字符表示该学生第j个题目的答案。
输出描述:一个正整数,全班学生最大的可能获得的分数总和。
示例1:
输入
2 4
ABCD
ABCE
1 2 3 4
输出
16
示例2:
输入
3 3
ABC
BCD
CDE
5 4 12
输出
21
思路:
需要求最好情况下全班能得到的分数,所以每道题对的学生数尽可能的多,所以取每道题中学生答案数最多的答案为标准答案,然后正确答案的数量乘上对应的分数,然后每道题的分数加起来就是最高分;
取正确答案方法:遍历学生成绩的字符串数组,然后依次取对应题号的答案在字符数组中,通过Collections.sort方法取出现次数最多的答案并且返回最多的次数;
代码:
public class Kuaishou1 {
public int grade(String[] str, int[] arr) {
int sum = 0;
for (int j = 0; j < arr.length; j++) {
char[] ch = new char[str.length];
for (int i = 0; i < str.length; i++) {
ch[i] = str[i].charAt(j);
}
int temp = gradeimpl(ch) * arr[j];
sum = sum + temp;
}
return sum;
}
public int gradeimpl(char[] charArray) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : charArray) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
Set<Entry<Character, Integer>> entrySet = map.entrySet();
Comparator<Entry<Character, Integer>> cr = new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) {
if (o1.getValue() > o2.getValue()) {
return 1;
} else if (o1.getValue() == o2.getValue()) {
return 0;
} else {
return -1;
}
}
};
List<Entry<Character, Integer>> list = new ArrayList<Entry<Character, Integer>>(entrySet);
Collections.sort(list, cr);
Entry<Character, Integer> e = list.get(list.size() - 1);
return e.getValue();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int arr[] = new int[m];
String[] str = new String[n];
for (int i = 0; i < n; i++) {
str[i] = in.next();
}
for (int j = 0; j < m; j++) {
arr[j] = in.nextInt();
}
Kuaishou1 test = new Kuaishou1();
System.out.println(test.grade(str, arr));
}
}