9.整理华子笔试

第一题

热点词,第一行输入topk和文章数M,接下来每两行代表是一篇文章,第一行代表标题,第二行代表正文,标题贡献的热度为3,正文贡献的热度为1,按照词语出现频率由高到低排列,如果相同,则标题中出现的词频率高的在前面,如果继续相同,按照词语在标题中出现的位置排列,继续相同按照词语在正文中出现的顺序排序

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int top = scanner.nextInt();
            int M=scanner.nextInt();
            String[] title = new String[M];
            String[] main = new String[M];
            scanner.nextLine();
            PriorityQueue<Map.Entry<String, Node>> queue = new PriorityQueue<>((a,b)->{
                return b.getValue().compare(a.getValue());
            });
            Map<String, Node> map = new HashMap<>();
            for (int i = 0; i < M; i++) {
                title[i]=scanner.nextLine();
                String[] ts = title[i].split("\\s+");
                for (int k = 0; k < ts.length; k++) {
                    if(!map.containsKey(ts[k])){
                        Node node = new Node(ts[k]);
                        node.firstIndexInTitle=k;
                        node.existInTitle++;
                        node.frequency+=3;
                        map.put(ts[k],node);
                    }else if(map.get(ts[k]).firstIndexInTitle>k){

                        map.get(ts[k]).firstIndexInTitle=k;
                        map.get(ts[k]).existInTitle++;
                        map.get(ts[k]).frequency+=3;
                    }else{
                        map.get(ts[k]).frequency+=3;
                        map.get(ts[k]).existInTitle++;
                    }
                }
                main[i]=scanner.nextLine();
                String[] ms = main[i].split("\\s+");
                for (int k = 0; k < ms.length; k++) {
                    if(!map.containsKey(ms[k])){
                        Node node = new Node(ms[k]);
                        node.firstIndexInMain=k;
                        node.frequency++;
                        map.put(ms[k],node);
                    }else if(map.get(ms[k]).firstIndexInMain>k){
                        map.get(ms[k]).firstIndexInMain=k;
                        map.get(ms[k]).frequency+=1;
                    }else{
                        map.get(ms[k]).frequency++;
                    }
                }
            }
            Set<Map.Entry<String, Node>> entries = map.entrySet();
            for (Map.Entry<String, Node> entry : entries) {
                queue.offer(entry);
            }
            while (top--!=0){
                System.out.print(queue.poll().getKey()+" ");
            }
        }

    }
}
class Node{
    String string;
    int existInTitle;
    int firstIndexInTitle=Integer.MAX_VALUE;
    int firstIndexInMain=Integer.MAX_VALUE;
    int frequency;
    public Node(String string) {
        this.string = string;
    }
    int compare(Node node){
        if(node.frequency!=this.frequency){
            return this.frequency-node.frequency;
        }else{
            if(node.existInTitle!=this.existInTitle){
                return this.existInTitle-node.existInTitle;
            }else{
                if(node.firstIndexInTitle!=this.firstIndexInTitle){
                    return node.firstIndexInTitle-this.firstIndexInTitle;
                }else{
                    return node.firstIndexInMain-this.firstIndexInMain;
                }
            }
        }
    }
}

测试输出:

5 2
4 1 2 3 1 4 5
3 3 3 1 1 1 3 3 3 4 4 4
5
5 5 5

输出:5 4 1 3 2

 第二题:

服务依赖问题,第一行输入两个数M,N,M代表0到M-1的编号,N代表开始的服务,后面的M行,第一个数代表有几个依赖,后面接着依赖的编号

import java.util.*;

public class Main {
    static Set<Integer> res=new HashSet<>();
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();//服务总数
        scanner.nextLine();
        int start=scanner.nextInt();//启动服务编号
        scanner.nextLine();
        //存放<编号,依赖集合>
        Map<Integer, Set<Integer>> map = new HashMap<>();
        for (int i = 0; i < N; i++) {
            String[] split = scanner.nextLine().split(",");
            int num=Integer.parseInt(split[0]);//依赖服务个数
            Set<Integer> set = new HashSet<>(num);
            for (int j = 1; j <split.length ; j++) {
                set.add(Integer.parseInt(split[j]));
            }
            map.put(i,set);
        }
        boolean[] used = new boolean[N];//服务是否已经遍历过
        List<Integer> list = new ArrayList<>();

        dfs(used, start,map);
        if(res.contains(-1)) {
            System.out.println(-1);
            return;
        }else{
            PriorityQueue<Object> queue = new PriorityQueue<>();
            for (Integer re : res) {
                queue.add(re);
            }
            while (!queue.isEmpty()){
                if(queue.size()==1){
                    System.out.print(queue.poll());
                    return;
                }
                System.out.print(queue.poll()+",");
            }
        }
    }

    private static void dfs(boolean[] used, int start, Map<Integer, Set<Integer>> map) {
        if(map.get(start).size()==0){
            res.add(start);
            return;
        }
        if(used[start]){
            res.add(-1);
            return;
        }
        used[start]=true;
        for (Integer integer : map.get(start)) {
            dfs(used,integer,map);
            res.add(integer);
        }

    }


}

测试输出

 

 第三题

 

//数组 长度 返回一共可以得到多少货物
    public int dealCargo(int[] arr, int N) {
        int max = dealArr(arr);
        if (max == 0) return 0;
        int res=0;
        //dp[i][j]表示的是对于位置[i]的平面[j]当前累加的长度
        int[][] dp = new int[arr.length][max + 1];
        //base
        for (int i = 0; i < arr.length; i++) {
            int height=arr[i];
            for (int j = 1; j <=height ; j++) {
                dp[i][j]=dp[i-1][j]+1;
                if(dp[i][j]==N){
                    res++;
                    dp[i][j]=0;
                }
            }
        }
        return res;
    }
 
    //处理数组 并且返回最大值
    public int dealArr(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < 0) {
                arr[i] = -arr[i];
            } else {
                arr[i] = 0;
            }
            max = Math.max(max, arr[i]);
        }
        return max;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值