2014 ACM/ICPC Xi'an Regional I - International Collegiate Routing Contest

本文探讨了一种特殊的网络路由问题,即如何找到给定路由表的补集,确保所有IPv4地址空间被覆盖且不与原表重叠。通过构建0-1Trie树并进行先序遍历来实现最小化补集路由表的设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目重现

You may know that Bluegao University (formly Bluey University) is famous of networking technology. One day, their headmaster Yuege received a special router, along with a task about routing table.
In this problem, routing table is a (probably) big table with several items, each item represents a subnet. The router has limited function, it can only deal with two next-hops and one main routing table. Packets will be send to next hop A if there exists a subnet containing the destination of the packet in the main outing table. Otherwise they will be send to next hop B.
You may know that, IPv4 uses 32-bit (four-byte) addresses, which limits the address space to 4294967296 (232) addresses. IPv4 addresses may be written in any notation expressing a 32-bit integer value, for human convenience, they are most often written in the dot-decimal notation, which consists of four octets of the address expressed individually in decimal and separated by periods. But their binary notation is also very useful. For example, IP address 128.2.142.23 can be expressed in dot-binary notation as 10000000.00000010.10001110.00010111. A subnet is a block of adjacent IP addresses with exactly same binary prefix, and usually written as the first IP address in its address space together with the bit length of prefix, like “202.120.224.0/24”. If an IP address is in the range of an subnet, we say that this subnet contains the IP address.
Yuege’s task is to invert the behaviour of his router, make all packets currently routed to hop A route to hop B, and vice versa. Also he wants to keep the size of the main routing table as small as possible, for performance.
In short, for a given routing table (i.e. a bunch of subnets), we need to get its “complement”, i.e. calculate a minimum set of subnets which have no intersection with given subnets, and their union must be the whole IPv4 address space.
Remember that Bluegao University is famous of networking tech, as headmaster of Bluegao University, Yuege definitely knows how to solve such problem, but he is too lazy to code, so he turns to you for help.

Input

The first line of the input gives the number of test cases, T . T test cases follow.
For each test case, the first line contains an integer n (0n30000), the number of subnets. Next n lines, each contains a subnet in the format of ‘a.b.c.d/l’, a,b,c,d,l are all integers, 0a,b,c,d256,0l32 .
Note that even if l=32 , the ‘/32’ part should not be omitted. And if l=0 , the IP address part must be ‘0.0.0.0’.

Output

For each test case, first output one line ‘Case #x:’, where x is the case number (starting from 1). Then on the second line print an integer n indicates the number of subnets in your answer. Next n lines each line contains a subnet in the same format as input. You may output them in any order.

Sample Input

3
0
1
0.0.0.0/1
1
128.0.0.0/1

Sample Output

Case #1:
1
0.0.0.0/0
Case #2:
1
128.0.0.0/1
Case #3:
1
0.0.0.0/1

题解

首先将IPv4地址看作一个0-1串。

对于a.b.c.d/ea, b, c, d 可转化为一个32-bit的0-1串,然后截取其中的前(高)e 位。

比如
0.0.0.0/200
192.168.1.1/811000000

按照这些0-1串构造字符集大小为2的字典树(0-1 Trie Tree)。

在串末尾代表的节点上打一个标记(在单词统计字典树中,这相当于统计数++)。

然后先序遍历一遍该树:
1. 遇到标记过的节点就返回
2. 遇到未标记过的节点有子节点为空则记录准备输出。

参考代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值