您的位置:首页 > 其它

ACM学习历程—BestCoder 2015百度之星资格赛1003 IP聚合(set容器)

2015-05-23 20:55 344 查看
Problem Description

当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址。网络地址等于子网掩码与 IP 地址按位进行与运算后的结果,例如:

子网掩码:A.B.C.D

IP 地址:a.b.c.d

网络地址:(A&a).(B&b).(C&c).(D&d)

Input

第一行包含一个整数T
,(1≤T≤50)
代表测试数据的组数,

接下来T
组测试数据。每组测试数据包含若干行,

第一行两个正整数N(1≤N≤1000,1≤M≤50),M
。接下来N
行,每行一个字符串,代表一个 IP 地址,

再接下来M
行,每行一个字符串代表子网掩码。IP 地址和子网掩码均采用 A.B.C.D
的形式,其中A、B、C、D
均为非负整数,且小于等于255。

Output

对于每组测试数据,输出两行:

第一行输出: "Case #i:" 。i
代表第i
组测试数据。

第二行输出测试数据的结果,对于每组数据中的每一个子网掩码,输出在此子网掩码下的网络地址的数量。

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


这个题目开个结构体直接用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\n", 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:\n", times);
Work();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: