CCF CSP python CIDR合并

因为超时,只拿到了90分,应该可以改到快很多,但是懒得改了

应该在写之前就好好想一下的

没有写注释偶


def compare(ipa,ipb):
    for i in range(4):
        if(ipa[i]<ipb[i]):
            return -1
        elif(ipa[i]>ipb[i]):
            return 1
        else:
            if(i==3):
                return 0
def iszero(a):
    if((a[0]*16777216+a[1]*65536+a[2]*256+a[3])%(2**(32-a[4]+1))==0):
        return 1
    else:
        return 0
def difone(a,b):
    aend = a[5]*16777216+a[6]*65536+a[7]*256+a[8]
    bbeg = b[0]*16777216+b[1]*65536+b[2]*256+b[3]
    if(aend+1==bbeg):
        return 1
    else:
        return 0

n = int(input())
cidr = []
for i in range(n):
    string = input()
    b=[]
    if(len(string.split('/'))==1): #省略长度型
        
        temp = list(map(int,string.split('.')))
        k = len(temp)
        for j in range(k):
            b.append(temp[j])
        for j in range(4-k):
            b.append(0)
        b.append(k*8)
    else: #省略后缀型与标准型
        t = string.split('/')
        te = int(t[1])
        tem = list(map(int,t[0].split('.')))
        tem.append(te)
        temp = tem
        k = len(temp)
        last = temp[-1]
        
        for j in range(k-1):
            b.append(temp[j])
        for j in range(4-k+1):
            b.append(0)
        b.append(temp[k-1])
    cidr.append(b)
cidr.sort(key = lambda x:(x[0],x[1],x[2],x[3],x[4]))
for x in cidr:
    adder =2**(32-x[4])-1
    a = adder%256
    b = (adder//256)%256
    c = (adder//65536)%256
    d = (adder//16777216)%256
    x.append(d+x[0])
    x.append(c+x[1])
    x.append(b+x[2])
    x.append(a+x[3])
cidr1=[]
while len(cidr)>1:
    if(compare(cidr[0][5:],cidr[1][5:])<0):
        cidr1.append(cidr[0])
        cidr.pop(0)
    else:
        cidr.pop(1)
cidr1.append(cidr[0])
cidr2 = []
while len(cidr1)>1:
    if(iszero(cidr1[0])==0):
        cidr2.append(cidr1[0])
        cidr1.pop(0)
    elif(cidr1[0][4]!=cidr1[1][4]):
        cidr2.append(cidr1[0])
        cidr1.pop(0)
    elif(difone(cidr1[0],cidr1[1])==0):
        cidr2.append(cidr1[0])
        cidr1.pop(0)
    else:
        cidr1[0][4]-=1
        cidr1[0][5:]=cidr1[1][5:]
        cidr1.pop(1)
cidr2.append(cidr1[0])

for x in cidr2:
    print('.'.join(map(str,x[0:4])),end='')
    print('/',end='')
    print(x[4])

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值