IP聚合

IP聚合
Accepts: 447
Submissions: 1135
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址。网络地址等于子网掩码与 IP 地址按位进行与运算后的结果,例如:
子网掩码:A.B.C.D
IP 地址:a.b.c.d
网络地址:(A&a).(B&b).(C&c).(D&d)
Input
第一行包含一个整数TTT,(1≤T≤50)(1 \leq T \leq 50)(1≤T≤50)代表测试数据的组数,
接下来TTT组测试数据。每组测试数据包含若干行,
第一行两个正整数N(1≤N≤1000,1≤M≤50),MN(1 \leq N \leq 1000, 1 \leq M \leq 50),MN(1≤N≤1000,1≤M≤50),M。接下来NNN行,每行一个字符串,代表一个 IP 地址,
再接下来MMM行,每行一个字符串代表子网掩码。IP 地址和子网掩码均采用 A.B.C.DA.B.C.DA.B.C.D的形式,其中A,B,C,DA,B,C,DA,B,C,D均为非负整数,且小于等于255。
Output
对于每组测试数据,输出两行:
第一行输出: "Case #i:" 。iii代表第iii组测试数据。
第二行输出测试数据的结果,对于每组数据中的每一个子网掩码,输出在此子网掩码下的网络地址的数量。
Sample Input
Copy
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
Copy
Case #1:
3
2
Case #2:
3
4
Statistic | Submit | Clarifications | Back
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#define MAXN 1050
typedef struct{int a,b,c,d;} ip;  /// ip
typedef struct{int A,B,C,D;} zw;  /// 子网掩码
typedef struct{int a,b,c,d;} recall;
ip IP[MAXN];zw ZW[MAXN];recall RECALL[MAXN];
int Count;
int cmp(const void *a,const void *b)  ///排序规则
{
    recall A  = *(recall *)a;recall B  = *(recall *)b;
    if(A.a!=B.a) return A.a - B.a;
    if(A.b!=B.b) return A.b - B.b;
    if(A.c!=B.c) return A.c - B.c;
    return A.d - B.d;
}
bool  CMP (int i,int j)
{
    int flag = 1;
    if(RECALL[i].a != RECALL[j].a ) flag = 0;
    if(RECALL[i].b != RECALL[j].b ) flag = 0;
    if(RECALL[i].c != RECALL[j].c ) flag = 0;
    if(RECALL[i].d != RECALL[j].d ) flag = 0;
    return flag;
}
int Cal(void)
{
    int ans = 0;
    for(int i=1,j=2;i<=Count;)
    {
        ans++;
        while(CMP(i,j)&&j<=Count) j++;
        i=j;j++;
    }
    return ans;
}
void Handle(int n,int m)
{
    for(int i=1;i<=n;i++) scanf("%d%*c%d%*c%d%*c%d",&IP[i].a,&IP[i].b,&IP[i].c,&IP[i].d);
    for(int i=1;i<=m;i++) scanf("%d%*c%d%*c%d%*c%d",&ZW[i].A,&ZW[i].B,&ZW[i].C,&ZW[i].D);
    for(int j=1;j<=m;j++)
    {
        Count = 0;
        for(int i=1;i<=n;i++)
        {
            RECALL[++Count].a =  ZW[j].A&IP[i].a;
            RECALL[Count].b =  ZW[j].B&IP[i].b;
            RECALL[Count].c =  ZW[j].C&IP[i].c;
            RECALL[Count].d =  ZW[j].D&IP[i].d;
        }
        qsort(RECALL+1,Count,sizeof(RECALL[0]),cmp);
        printf("%d\n",Cal());
    }
}
int main(void)
{
   // freopen("D:\\test.txt","r",stdin);
   // freopen("D:\\tsst.txt","w",stdout);
    int T;scanf("%d",&T);
    for(int i=1,N,M;i<=T&&(scanf("%d %d",&N,&M));i++)
        {printf("Case #%d:\n",i);Handle(N,M);}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值