百度程序设计大赛1003IP聚合

主要没什么,只是涉及到了一个结构体的排序就OK了。

唯一的问题就是提交的时候WA了3次,还以为是算法错了,结果数组开到1e5就过了!!!

测试数据居然比给的范围要大!!!

测试数据居然比给的范围要大!!!

测试数据居然比给的范围要大!!!

(关键问题说3遍)

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

struct node{
    int a;
    int b;
    int c;
    int d;
}a[10550],b[2000],ans[2000],temp;

bool cmp(node x, node y){
    if(x.a != y.a)  return x.a<=y.a;
    if(x.b != y.b)  return x.b<=y.b;
    if(x.c != y.c)  return x.c<=y.c;
    if(x.d != y.d)  return x.d<=y.d;
}

int main(){
    int T;
    int cnt;
    //freopen("in.txt","r",stdin);
    scanf(" %d",&T);
    for(int x = 1;x <= T;x++){
        int N,M;
        scanf(" %d %d",&N,&M);
        for(int j = 0;j < N;j++)
            scanf(" %d.%d.%d.%d",&a[j].a,&a[j].b,&a[j].c,&a[j].d);//IP地址
        for(int i = 0;i < M;i++)
            scanf(" %d.%d.%d.%d",&b[i].a,&b[i].b,&b[i].c,&b[i].d);//子网掩码
        printf("Case #%d:\n",x);

        for(int i = 0;i < M;i++){
            for(int j = 0;j < N;j++){
                ans[j].a = a[j].a&b[i].a;
                ans[j].b = a[j].b&b[i].b;
                ans[j].c = a[j].c&b[i].c;
                ans[j].d = a[j].d&b[i].d;
            }
            sort(ans,ans+N,cmp);

            cnt = N;
            for(int j = 1;j < N;j++){
                temp = ans[j-1];
                if(temp.a == ans[j].a && temp.b == ans[j].b && temp.c == ans[j].c && temp.d == ans[j].d)
                    cnt--;
            }
            printf("%d\n",cnt);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值