# 2016"百度之星" - 测试赛 1003 IP聚合（Map存储）

2016-05-12

Accepts: 341
Submissions: 810
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others) Problem Description

IP 地址：a.b.c.d

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>

using namespace std;
const int N=1e5+5;
map<long long,int>V;

struct node
{
int a,b,c,d;
} ip[N],sub[N],net[N];

int main()
{
int t,ans,cas=1;
long long q;
scanf("%d",&t);
while (t--)
{
//ans=0;
memset(net,0,sizeof(net));
int n,m;
scanf("%d%d",&n,&m);
for (int i=0; i<n; i++)
{
scanf("%d.%d.%d.%d",&ip[i].a,&ip[i].b,&ip[i].c,&ip[i].d);
}
for (int i=0; i<m; i++)
{
scanf("%d.%d.%d.%d",&sub[i].a,&sub[i].b,&sub[i].c,&sub[i].d);
}

printf("Case #%d:\n",cas++);
for (int i=0; i<m; i++)
{
V.clear();
int k=0;
ans=0;
for (int j=0; j<n; j++)
{
net[k].a=ip[j].a&sub[i].a;
net[k].b=ip[j].b&sub[i].b;
net[k].c=ip[j].c&sub[i].c;
net[k].d=ip[j].d&sub[i].d;
q=net[k].a*1000000000+net[k].b*1000000+net[k].c*1000+net[k].d;
if (!V[q])
{
ans++;
V[q]=1;
}
k++;
}
printf ("%d\n",ans);
}
}
return 0;
}