字节题是真顶
没办法把数组作为key就把数组转换成String
import java.util.Scanner; import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); HashMap<String,ArrayList<Integer>> hash = new HashMap<>(); int N = sc.nextInt(); for (int k=0;k<N;k++){ int n = sc.nextInt(); for(int i=0;i<n;i ++){ int m = sc.nextInt(); for(int j=0;j<m;j++){ String key = sc.nextInt() + "_" +sc.nextInt(); if(hash.containsKey(key)){ ArrayList<Integer> list = hash.get(key); list.add(i); }else{ ArrayList<Integer> new_list = new ArrayList<Integer>(); new_list.add(i); hash.put(key,new_list); } } } int MAX = 1; for(String key: hash.keySet()){ int max = 1; ArrayList<Integer> list = hash.get(key); for (int i =1;i< list.size();i++){ if(list.get(i) == list.get(i-1)+1) { max++; MAX = Math.max(MAX,max); } else{ MAX = Math.max(MAX,max); max = 1; } } } if (hash.size()==0) System.out.println(0); else System.out.println(MAX); } } }
自己构建hash的算法
链接:https://www.nowcoder.com/questionTerminal/5afcf93c419a4aa793e9b325d01957e2 来源:牛客网 import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int ans = 0; Map<Reference, Integer> map1 = new HashMap<Reference, Integer>(); Map<Reference, Integer> map2 = new HashMap<Reference, Integer>(); for(int i=0;i<n;i++) { int m = scanner.nextInt(); map1.clear(); for(int j=0;j<m;j++) { int p = scanner.nextInt(); if(p==0) { map1.clear(); continue; } for(int k=0;k<p;k++) { int x = scanner.nextInt(); int y = scanner.nextInt(); Reference reference = new Reference(x, y); if(map1.containsKey(reference)) { map2.put(reference, map1.get(reference)+1); }else { map2.put(reference, 1); } ans = Math.max(ans, map2.get(reference)); } map1.clear(); map1.putAll(map2); map2.clear(); } } System.out.println(ans); } } class Reference{ int x; int y; public Reference(int x, int y) { super(); this.x = x; this.y = y; } @Override public int hashCode() { return x + y + x*y; } @Override public boolean equals(Object obj) { Reference reference = (Reference)obj; return x==reference.x && y==reference.y; } }