这个题目开个结构体直接用set计数就OK。不过需要注意的是需要对运算符进行重载。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include <set> #include <map> #include <queue> #include <string> #define LL long long using namespace std; struct IP { int a, b, c, d; bool operator < (const IP x) const { if (a != x.a) return a < x.a; else if (b != x.b) return b < x.b; else if (c != x.c) return c < x.c; else if (d != x.d) return d < x.d; return false; } }; int n, m; IP f[1005], t; void Input() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d.%d.%d.%d", &f[i].a, &f[i].b, &f[i].c, &f[i].d); } void Work() { IP k; for (int i = 0; i < m; ++i) { set <IP> s; scanf("%d.%d.%d.%d", &t.a, &t.b, &t.c, &t.d); for (int j = 0; j < n; ++j) { k.a = f[j].a & t.a; k.b = f[j].b & t.b; k.c = f[j].c & t.c; k.d = f[j].d & t.d; s.insert(k); } printf("%d ", s.size()); } } int main() { //freopen("test.in", "r", stdin); int T; scanf("%d", &T); for (int times = 1; times <= T; ++times) { Input(); printf("Case #%d: ", times); Work(); } return 0; }