加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

百度之星资格赛--IP聚合

发布时间:2020-12-13 22:27:23 所属栏目:百科 来源:网络整理
导读:每一个IP循环一遍子网掩码,用结构体十分方便。 就是重载运算符的问题,要特别注意。 #includecstdio#includeset#includealgorithmusing namespace std;struct IP{ int a,b,c,d;// bool operator == (const IPrhs) const// {// return a==rhs.ab==rhs.bc==rh

每一个IP循环一遍子网掩码,用结构体十分方便。

就是重载运算符的问题,要特别注意。

#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
struct IP
{
    int a,b,c,d;
//    bool operator == (const IP&rhs) const
//    {
//        return a==rhs.a&&b==rhs.b&&c==rhs.c&&d==rhs.d;
//    }
    bool operator < (const IP&rhs) const
    {
        if(a!=rhs.a) return a<rhs.a;
        else if(b!=rhs.b) return b<rhs.b;
        else if(c!=rhs.c) return c<rhs.c;
        else return d<rhs.d;
    }
    IP& operator & (IP& rhs)
    {
        IP x;
        x.a = a&rhs.a;
        x.b = b&rhs.b;
        x.c = c&rhs.c;
        x.d = d&rhs.d;
        return x;
    }
}ZI[1000 + 10];

int main()
{
    int n,N,M,kase=0;
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&N,&M);
        for(int i=0;i<N;i++)
            scanf("%d.%d.%d.%d",&ZI[i].a,&ZI[i].b,&ZI[i].c,&ZI[i].d);
        printf("Case #%d:n",++kase);
        for(int i=0;i<M;i++){
            IP IIPP;
        scanf("%d.%d.%d.%d",&IIPP.a,&IIPP.b,&IIPP.c,&IIPP.d);
//            scanf("%d.%d.%d.%d",&IPDI[i].a,&IPDI[i].b,&IPDI[i].c,&IPDI[i].d);
            int count = 0;
            set<IP> dict;
            for(int j=0;j<N;j++){
                IP KO = IIPP&ZI[j];
//            printf("%d.%d.%d.%dn",KO.a,KO.b,KO.c,KO.d);
//            printf("IIPP:%d.%d.%d.%dn",IIPP.a,IIPP.b,IIPP.c,IIPP.d);
                if( !dict.count(KO) ) { count++; dict.insert(KO); }
            }
            printf("%dn",count);
        }

    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读