华为机试2022.4.6——“查找舆情热词”

题目介绍

输入一篇文章的标题和正文,对文章中出现的词语进行处理,输出频率最高的 N 个词。标题中的出现的词权重系数为3,正文中的词权重是1;返回的答案按照频率从高到低排序,如果词频相同,标题中频率高的在前面;如果标题中词频相同,按照标题中出现的先后顺序排序,先出现的排在前面;如果任然相同,按照词语在正文中出现的先后顺序进行排序,先出现的在前面。


代码

package huawei;

import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 * 使用一行输出出现频率最高的 topN 个词语,每个词语以“ ”隔开。标题中出现的词语频率系数为3,
 * 正文中出现的词语频率系数为1,返回的答案按照词语出现频率由高到低排序,当词语出现的频率相同时,
 * 在标题中出现的频率次数高的排在前面;如果仍然相同,则按照词语在标题中出现的先后顺序进行排序,
 * 先出现的排在前面;如果仍然相同,则按照词语在正文中出现的先后顺序进行排序,先出现的排在前面
 *
 * @author Sandalphon
 * @date 2022/08/17 15:04
 **/
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int topN = sc.nextInt();
        int m = sc.nextInt();
        String[] title = new String[m];
        String[] content = new String[m];
        sc.nextLine();
        for (int i = 0; i < m; i++) {
            title[i] = sc.nextLine();
            content[i] = sc.nextLine();
        }
        String[] ans = solve(topN, m, title, content);
        System.out.println(Arrays.toString(ans));
    }

    private static String @NotNull [] solve(int topN, int m, String[] title, String[] content) {
        HashMap<String, Word> map = new HashMap<>();
        for (String t : title) {
            String[] words = t.split(" ");
            for (int i = 0; i < words.length; i++) {
                String word = words[i];
                if (!map.containsKey(word)) {
                    Word w = new Word(word);
                    w.totalFreq += 3;
                    w.titleFreq += 3;
                    w.titleOrder = i;
                    map.put(word, w);
                } else {
                    Word w = map.get(word);
                    w.totalFreq += 3;
                    w.titleFreq += 3;
                    map.put(word, w);
                }
            }
        }
        for (String t : content) {
            String[] contents = t.split(" ");
            for (int i = 0; i < contents.length; i++) {
                String c = contents[i];
                if (!map.containsKey(c)) {
                    Word w = new Word(c);
                    w.totalFreq += 1;
                    w.contentOrder = i;
                    map.put(c, w);
                } else {
                    Word w = map.get(c);
                    w.totalFreq += 1;
                    w.contentOrder = Math.min(w.contentOrder, i);
                    map.put(c, w);
                }
            }
        }
        List<Word> list = new LinkedList<>();
        for (Word w : map.values()) {
            list.add(w);
        }
        Collections.sort(list, new Comparator<Word>() {
            @Override
            public int compare(Word o1, Word o2) {
                if (o1.totalFreq != o2.totalFreq) {
                    return o2.totalFreq - o1.totalFreq;
                } else {
                    if (o1.titleFreq != o2.titleFreq) {
                        return o2.titleFreq - o1.totalFreq;
                    } else {
                        if (o1.titleOrder != o2.titleOrder) {
                            return o1.titleOrder - o2.titleOrder;
                        } else {
                            return o1.contentOrder - o2.contentOrder;
                        }
                    }
                }
            }
        });
        String[] ans = new String[topN];
        for (int i = 0; i < topN; i++) {
            ans[i] = list.get(i).val;
        }
        return ans;
    }

    private static class Word {
        String val;
        int totalFreq;
        int titleFreq;
        int titleOrder;
        int contentOrder;

        public Word(String val) {
            this.val = val;
            totalFreq = 0;
            titleFreq = 0;
            titleOrder = Integer.MAX_VALUE;
            contentOrder = Integer.MAX_VALUE;
        }
    }
}

测试用例

算法输入

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 zhhengti kongzhi lianghao
xinguan yimiao linchuang shiyan
wuzhong xinguan yimiao tongguo sanqi linchuang shiyan xiaoguo lianghao

算法输出

xinguan xinzeng bendi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值