IP聚合
Accepts: 1898
Submissions: 4972
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Input
第一行包含一个整数
接下来
第一行两个正整数
再接下来
Output
Sample Input
2 5 2 192.168.1.0 192.168.1.101 192.168.2.5 192.168.2.7 202.14.27.235 255.255.255.0 255.255.0.0 4 2 127.127.0.1 10.134.52.0 127.0.10.1 10.134.0.2 235.235.0.0 1.57.16.0
Sample Output
Case #1: 3 2 Case #2: 3 4
解题思路: 可以直接模拟,关键用java注意超时、
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int p = 0; p < n; p++) { int i1 = sc.nextInt(); int i2 = sc.nextInt(); sc.nextLine(); String a1[] = new String[i1]; String a2[] = new String[i2]; for (int i = 0; i < a1.length; i++) a1[i]=sc.nextLine(); for (int i = 0; i < a2.length; i++) a2[i]=sc.nextLine(); System.out.println("Case #"+(p+1)+":"); for (int i = 0; i < i2; i++) { String s[] = a2[i].split("[.]"); int A = Integer.parseInt(s[0]); int B = Integer.parseInt(s[1]); int C = Integer.parseInt(s[2]); int D = Integer.parseInt(s[3]); Set<String>list=new HashSet<String>(); for (int j = 0; j < i1; j++) { s = a1[j].split("[.]"); int a = Integer.parseInt(s[0]); int b = Integer.parseInt(s[1]); int c = Integer.parseInt(s[2]); int d = Integer.parseInt(s[3]); StringBuffer sb = new StringBuffer(); sb.append(a&A).append(".").append(b&B).append(".").append(c&C).append(".").append(d&D); list.add(sb.toString()); } System.out.println(list.size()); } } } }