前端之家收集整理的这篇文章主要介绍了
2016 百度之星 IP聚合,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<span style="font-family:Courier New;font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int a,b,c,d;
}ip[1005],sn[55],res[1005];
/*int cmp(struct node x,struct node y)
{
if(x.a != y.a)
return x.a < y.a;
else if(x.b != y.b)
return x.b < y.b;
else if(x.c != y.c)
return x.c < y.c;
else
return x.d < y.d;
}*/
int cmp( struct node x,struct node y)
{
if(x.a != y.a)
return x.a < y.a ? 1:0;
else if(x.b != y.b)
return x.b < y.b?1: 0;
else if(x.c != y.c)
return x.c < y.c?1 : 0;
else
return x.d < y.d? 1 : 0;
}
int main()
{
int T,i,j,n,m,k;
cin >> T;
int cnt = 0;
while(T --)
{
cnt ++;
cin >> n >>m;//n代表ip,m代表网关
for(i=1; i<=n; i++)
{
scanf("%d.%d.%d.%d",&ip[i].a,&ip[i].b,&ip[i].c,&ip[i].d);
}
for(i=1; i<=m; i++)
scanf("%d.%d.%d.%d",&sn[i].a,&sn[i].b,&sn[i].c,&sn[i].d);
printf("Case #%d:\n",cnt);
for(i=1; i<=m; i++)
{
k = 0;
for(j=1; j<=n; j++)
{
res[k].a = ip[j].a & sn[i].a;
res[k].b = ip[j].b & sn[i].b;
res[k].c = ip[j].c & sn[i].c;
res[k++].d = ip[j].d & sn[i].d;
}
/* cout << " %%%%%%%%%%%%%% " << endl;
for(int p=0; p<k; p++)
{
cout << res[p].a << "." << res[p].b << "." << res[p].c << "." << res[p].d << endl;
}
cout << " %%%%%%%%%%%%%% " << endl;
*/
sort(res,res+k,cmp);
//qsort(res,k,sizeof(struct node),cmp);
/* cout << " ************ " << endl;
for(int p=0; p<k; p++)
{
cout << res[p].a << "." << res[p].b << "." << res[p].c << "." << res[p].d << endl;
}
cout << " ************ " << endl;
*/
int ans = 1;
for(int t = 1; t<k; t++)
{
if(res[t].a != res[t-1].a || res[t].b != res[t-1].b || res[t].c != res[t-1].c || res[t].d != res[t-1].d)
ans++;
}
cout << ans << endl;
}
}
return 0;
} </span>