Java——攻击日志统计(HashMap)

攻击日志统计,根据查询的起始时间、终止时间以及攻击类型三个参数进行筛选统计,获得总攻击次数

在这里插入图片描述
在这里插入图片描述
本题在分析的过程中默认攻击日志的时间戳是唯一的,因此解答的算法没有通过平台的测试,只提供一个思路,供参考

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		int res[] = new int[m];
		HashMap<Integer, HashMap<String, Integer>> hp = new HashMap<>();
		for(int i=0;i<n;i++) {
    
    
			HashMap<String, Integer> inside = new HashMap<>();
			int time = scanner.nextInt();
			String type = scanner.next();
			int num = scanner.nextInt();
			inside.put(type, num);
			hp.put(time, inside);
		}
		//对键进行排序
		Set<Integer> keys=hp.keySet();
		for(int i=0;i<m;i++) {
    
    
			int start = scanner.nextInt();
			int end = scanner.nextInt();
			String type = scanner.next();
			for(int key:keys) {
    
    
				if(key>=start&&key<=end) {
    
    
					if(hp.get(key).containsKey(type)) {
    
    
						res[i]+=hp.get(key).get(type);	
					}
				}
			}
		}
		
		for(int i=0;i<m;i++) {
    
    
			System.out.println(res[i]);
		}
		scanner.close();
	}
}

猜你喜欢

转载自blog.csdn.net/WU2629409421perfect/article/details/112299407
今日推荐