2022年4月6日,华为笔试题一:词频统计
(回忆)
输入:
第一行两个数,表示topN和M,topN是需要统计的前topN个高频词,M表示有M组标题和正文。
第二行开始,一共M组,每组两行,一行标题一行正文、一行标题一行正文、…。
还有个条件:标题中的频次系数是3,正文中的频次系数是1。意思是标题中出现的次数一次算三次,正文中出现一次就算一次。
输出:
输出统计的出现频次最多的topN个单词,空格隔开。如果出现次数相同,出现在标题中的排在前面,如果还相同,按出现顺序先出现的排在前面。
import java.util.*;
//input sample 1:
//3 2
//xinguan feiyan xinzeng bendi quezhen anli
//ju baodao chengdu xinzeng xinguan feiyan bendi quezhen anli yili shenzhen xinzeng bendi quezhen anli liangli yiqing zhengti kongzhi lianghao
//xinguan yimiao linchuang shiyan
//wuzhong xinguan yimiao tongguo sanqi linchaung shiyan xiaoguo lianghao
//
//output:
//xinguan xinzeng bendi
// We have imported the necessary tool classes.
// If you need to import additional packages or classes, please import here.
public class Main {
public static void main(String[] args) {
// please define the JAVA input here. For example: Scanner s = new Scanner(System.in);
// please finish the function body here.
// please define the JAVA output here. For example: System.out.println(s.nextInt());
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
String topN_M = s.nextLine();
int topN = Integer.parseInt(topN_M.split(" ")[0]);
int M = Integer.parseInt(topN_M.split(" ")[1]);
Map<String, int[]> map = new HashMap<>();
int titleOrText;
int order = 1;
for (int m = 0; m < M; m++) {
String title = s.nextLine();
String[] titleArr = title.split(" ");
String article = s.nextLine();
String[] articleArr = article.split(" ");
titleOrText = 1;
for (String ss : titleArr) {
if (map.containsKey(ss)) {
int[] v = map.get(ss);
int c = v[0] + 3;
v[0] = c;
map.put(ss, v);
} else {
int[] v = new int[3];
v[0] = 3;
v[1] = titleOrText;
v[2] = order;
order++;
map.put(ss, v);
}
}
titleOrText = 2;
for (String ss : articleArr) {
if (map.containsKey(ss)) {
int[] v = map.get(ss);
int c = v[0] + 1;
v[0] = c;
map.put(ss, v);
} else {
int[] v = new int[3];
v[0] = 1;
v[1] = titleOrText;
v[2] = order;
order++;
map.put(ss, v);
}
}
}
Set<Map.Entry<String, int[]>> entrySet = map.entrySet();
List<Map.Entry<String, int[]>> entryList = new ArrayList<>(entrySet);
entryList.sort((o1, o2) -> {
int num = o2.getValue()[0] - o1.getValue()[0];
if (num == 0) {
num = o1.getValue()[1] - o2.getValue()[1];
}
if (num == 0) {
num = o1.getValue()[2] - o2.getValue()[2];
}
return num;
});
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, int[]> entry : entryList) {
if (topN > 0) {
sb.append(entry.getKey()).append(" ");
topN--;
} else {
break;
}
}
String s1 = sb.toString();
System.out.println(s1.substring(0, s1.length() - 1));
}
}
}
【拙劣解法,欢迎指教】