每一个IP循环一遍子网掩码,用结构体十分方便。
就是重载运算符的问题,要特别注意。
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
struct IP
{
int a,b,c,d;
// bool operator == (const IP&rhs) const
// {
// return a==rhs.a&&b==rhs.b&&c==rhs.c&&d==rhs.d;
// }
bool operator < (const IP&rhs) const
{
if(a!=rhs.a) return a<rhs.a;
else if(b!=rhs.b) return b<rhs.b;
else if(c!=rhs.c) return c<rhs.c;
else return d<rhs.d;
}
IP& operator & (IP& rhs)
{
IP x;
x.a = a&rhs.a;
x.b = b&rhs.b;
x.c = c&rhs.c;
x.d = d&rhs.d;
return x;
}
}ZI[1000 + 10];
int main()
{
int n,N,M,kase=0;
scanf("%d",&n);
while(n--){
scanf("%d%d",&N,&M);
for(int i=0;i<N;i++)
scanf("%d.%d.%d.%d",&ZI[i].a,&ZI[i].b,&ZI[i].c,&ZI[i].d);
printf("Case #%d:\n",++kase);
for(int i=0;i<M;i++){
IP IIPP;
scanf("%d.%d.%d.%d",&IIPP.a,&IIPP.b,&IIPP.c,&IIPP.d);
// scanf("%d.%d.%d.%d",&IPDI[i].a,&IPDI[i].b,&IPDI[i].c,&IPDI[i].d);
int count = 0;
set<IP> dict;
for(int j=0;j<N;j++){
IP KO = IIPP&ZI[j];
// printf("%d.%d.%d.%d\n",KO.a,KO.b,KO.c,KO.d);
// printf("IIPP:%d.%d.%d.%d\n",IIPP.a,IIPP.b,IIPP.c,IIPP.d);
if( !dict.count(KO) ) { count++; dict.insert(KO); }
}
printf("%d\n",count);
}
}
return 0;
}