题解 | #特征提取#
特征提取
https://www.nowcoder.com/practice/5afcf93c419a4aa793e9b325d01957e2
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static class Node{ int a; int b; public Node(int a, int b){ this.a=a; this.b=b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return a == node.a && b == node.b; } @Override public int hashCode() { return 31 * a + b; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int numberOfTest = in.nextInt(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int max=0; int numberOfZhen=in.nextInt(); Map<Node, Integer> map=new HashMap<>(); //in one test case, for each line for(int i=0;i<numberOfZhen;i++){ int numberOfVec=in.nextInt(); Set<Node> set=new HashSet<>(); // in one line, iterate each vector for(int j=0;j<numberOfVec;j++){ int a=in.nextInt(); int b=in.nextInt(); Node node=new Node(a,b); set.add(node); if(map.containsKey(node)){ map.put(node, map.get(node)+1); max=Math.max(max, map.get(node)); }else{ map.put(node, 1); } } Set<Node> nodesToRemove = new HashSet<>(); for (Node n : map.keySet()) { if (!set.contains(n)) { nodesToRemove.add(n); } } // 在循环外部删除元素 for (Node n : nodesToRemove) { map.remove(n); } } if(max<2){ System.out.println(1); }else{ System.out.println(max); } } } }