华为笔试题:词频统计

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));
        }
    }
}

【拙劣解法,欢迎指教】

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值