CCFCSP 201812-3 CIDR合并(90分超时)

思路

  • 没啥可说的,根据题目要求写就好
  • 由于比较菜又很懒,所以超时了,就懒得再继续做了
  • 这次给我最大的体验就是好好看测试样例,一开始一直0分,看了测试样例才知道感情我排序那一步都没对,才改了cmp函数。

代码(90分超时)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct ip{
    int four[4];
    int len;
};

bool cmp(ip ad1,ip ad2){
    for(int i=0;i<4;i++){
        if(ad1.four[i]!=ad2.four[i])
            return ad1.four[i]<ad2.four[i];
    }
    return ad1.len<ad2.len;
}

int compare(ip ad1,ip ad2){
    int min=ad1.len;
    int first=min/8;
    int second=min%8;
    int flag=1;
    for(int j=0;j<first;j++){
        if(ad1.four[j]!=ad2.four[j]){
            flag=0;
            break;
        }
    }
    if(flag==1){
        int small=ad1.four[first];
        int big=ad2.four[first];
        int top=128;
        for(int j=0;j<second;j++){
            if(small/top!=big/top){
                flag=0;
                break;
            }
            small%=top;
            big%=top;
            top/=2;
        }
         
    }
    return flag;
}

int main(){
    ios::sync_with_stdio();
    int n;
    cin>>n;
    vector<ip> save;
    for(int i=0;i<n;i++){
        string temp;
        cin>>temp;
        int nowip=0;
        int coupoint=0;
        ip nowadd;
        int flag=0;
        for(int j=0;j<temp.size();j++){
            if(temp[j]>='0' && temp[j]<='9'){
                nowip=nowip*10+temp[j]-'0';
            }
            else if(temp[j]=='.'){
                nowadd.four[coupoint]=nowip;
                nowip=0;
                coupoint++;
            }
            else if(temp[j]=='/'){
                flag=1;
                nowadd.four[coupoint]=nowip;
                coupoint++;
                int nowlength=0;
                
                j++;
                
                while(j<temp.size() && temp[j]>='0' && temp[j]<='9'){
                    
                    nowlength=nowlength*10+temp[j]-'0';
                    //cout<<temp[j]<<endl;
                    j++;
                }
                //cout<<nowlength<<endl;
                nowadd.len=nowlength;
            }
        }

        if(flag==0){
            nowadd.four[coupoint]=nowip;
            coupoint++;
            nowadd.len=8*coupoint;
        }
        
        
        if(coupoint<4){
            while(coupoint<4){
                nowadd.four[coupoint]=0;
                coupoint++;
            }
        }
        save.push_back(nowadd);
    }
    
    sort(save.begin(),save.end(),cmp);
    
    
    for(int i=1;i<save.size();i++){
        if(i<1)
            continue;
        
        int flag=compare(save[i-1],save[i]);
        
        if(flag==1){
            save.erase(save.begin()+i);
            i--;
        }
        
    }
    
    sort(save.begin(),save.end(),cmp);

    
    for(int i=1;i<save.size();i++){
        if(i<1)
            continue;
        if(save[i-1].len==save[i].len){//equal

            ip adda;
            int k=4;
            while(k--){

                adda.four[k]=save[i-1].four[k];
            }
            adda.len=save[i-1].len-1;

            if(compare(adda,save[i-1]) && compare(adda,save[i])){
                save[i-1]=adda;
                save.erase(save.begin()+i);
                i--;
                i--;
            }
            
        }
    }
    
    for(int i=0;i<save.size();i++){
        cout<<save[i].four[0]<<"."<<save[i].four[1]<<"."<<save[i].four[2]<<"."<<save[i].four[3]<<"/"<<save[i].len<<endl;
    }
    
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值