2015年百度之星程序设计大赛 - 资格赛:1003 IP聚合

IP聚合

 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

第一行包含一个整数 T 1T50 代表测试数据的组数,

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

第一行两个正整数 N1N1000,1M50,M 。接下来 N 行,每行一个字符串,代表一个 IP 地址,

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

分析:

大体题意:两个数(子网掩码和IP)进行与操作后去重。主要难在去重方法上,两种方法解决去重:第一种是把每一个位置的数组合成一个12位数的一个数,然后进行判断是否相等;第二种就是用结构体的方法,每一个位置的数进行判断。


LANGUAGE:C++

CODE1:

<span style="font-size:18px;"><strong>#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;

struct node{
    int a,b,c,d;
};

int main()
{
    int t,cnt=0;
    cin>>t;
    while(t--){
        int n,m;
        cnt++;
        cin>>n>>m;
        node p[1005];
        long long ans[1005];
        memset(p,0,sizeof(p));
        for(int i=0;i<n;i++)
            scanf("%d.%d.%d.%d",&p[i].a,&p[i].b,&p[i].c,&p[i].d);
        int a,b,c,d;
        cout<<"Case #"<<cnt<<':'<<endl;
        for(int i=0;i<m;i++){
            int res=0;
            memset(ans,0,sizeof(ans));
            scanf("%d.%d.%d.%d",&a,&b,&c,&d);
            for(int j=0;j<n;j++){
                ans[res++]=(a&p[j].a)*1e9+(b&p[j].b)*1e6+(c&p[j].c)*1e3+(d&p[j].d);
//                cout<<ans[res-1]<<endl;
            }
            if(res==1){
                cout<<1<<endl;
                continue;
            }
            sort(ans,ans+res);
            int sum=1;
            for(int i=1;i<res;i++){
                if(ans[i]!=ans[i-1])
                    sum++;
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}
</strong></span>

CODE2:

<span style="font-size:18px;"><strong>#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct node{
    int a,b,c,d;
};

int cmp(node x,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;
}

bool judge(node x,node y)
{
    if(x.a==y.a&&x.b==y.b&&x.c==y.c&&x.d==y.d)
        return false;
    return true;
}

int main()
{
    int t,cnt=0;
    cin>>t;
    while(t--){
        int n,m;
        cnt++;
        cin>>n>>m;
        node p[1005];
        for(int i=0;i<n;i++)
            scanf("%d.%d.%d.%d",&p[i].a,&p[i].b,&p[i].c,&p[i].d);
        int a,b,c,d;
        cout<<"Case #"<<cnt<<':'<<endl;
        for(int i=0;i<m;i++){
            int res=0;
            node ans[1005];
            scanf("%d.%d.%d.%d",&a,&b,&c,&d);
            for(int j=0;j<n;j++){
                ans[res].a=a&p[j].a;
                ans[res].b=b&p[j].b;
                ans[res].c=c&p[j].c;
                ans[res].d=d&p[j].d;
                res++;
            }
            if(res==1){
                cout<<1<<endl;
                continue;
            }
            sort(ans,ans+res,cmp);
            int sum=1;
            for(int i=1;i<res;i++){
                if(judge(ans[i],ans[i+1]))
                    sum++;
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}
</strong></span>



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值