模拟 百度之星资格赛 1003 IP聚合

 

题目传送门

 1 /*  2  模拟水题,排序后找出重复的ip就可以了  3 */  4 #include <cstdio>  5 #include <iostream>  6 #include <algorithm>  7 #include <cstring>  8 #include <string>  9 #include <cmath> 10 using namespace std; 11 12 const int MAXN = 1e3 + 10; 13 const int INF = 0x3f3f3f3f; 14 struct Ip 15 { 16 int a, b, c, d; 17 }ip[MAXN]; 18 struct H 19 { 20 int a, b, c, d; 21 }h[MAXN]; 22 int ans[55]; 23 24 bool cmp(H x, H y) 25 { 26 if (x.a == y.a) 27  { 28 if (x.b == y.b) 29  { 30 if (x.c == y.c) return x.d < y.d; 31 else return x.c < y.c; 32  } 33 else return x.b < y.b; 34  } 35 else return x.a < y.a; 36 } 37 38 int main(void) //百度之星资格赛 1003 IP聚合 39 { 40 int t, cas = 0; 41 scanf ("%d", &t); 42 while (t--) 43  { 44 int n, m; scanf ("%d%d", &n, &m); 45 for (int i=1; i<=n; ++i) 46 scanf ("%d.%d.%d.%d", &ip[i].a, &ip[i].b, &ip[i].c, &ip[i].d); 47 48 memset (ans, 0, sizeof (ans)); 49 for (int i=1; i<=m; ++i) 50  { 51 int u, v, w, x; 52 scanf ("%d.%d.%d.%d", &u, &v, &w, &x); 53 for (int j=1; j<=n; ++j) 54  { 55 h[j].a = (ip[j].a & u); h[j].b = (ip[j].b & v); 56 h[j].c = (ip[j].c & w); h[j].d = (ip[j].d & x); 57  } 58 59 sort (h+1, h+1+n, cmp); 60 61 int cnt = 0; 62 for (int k=2; k<=n; ++k) 63  { 64 if (h[k].a == h[k-1].a && 65 h[k].b == h[k-1].b && 66 h[k].c == h[k-1].c && 67 h[k].d == h[k-1].d) cnt++; 68  } 69 70 ans[i] = (n - cnt); 71  } 72 73 printf ("Case #%d:\n", ++cas); 74 for (int i=1; i<=m; ++i) printf ("%d\n", ans[i]); 75  } 76 77 return 0; 78 } 79 80 /* 81 2 82 5 2 83 192.168.1.0 84 192.168.1.101 85 192.168.2.5 86 192.168.2.7 87 202.14.27.235 88 255.255.255.0 89 255.255.0.0 90 4 2 91 127.127.0.1 92 10.134.52.0 93 127.0.10.1 94 10.134.0.2 95 235.235.0.0 96 1.57.16.0 97 */

 

你可能感兴趣的:(百度)