CF GYM 100548 International Collegiate Routing Contest(2014ACM西安现场赛Problem I)不懂提

来源:互联网 发布:卖二手衣服的软件 编辑:程序博客网 时间:2024/06/07 00:12

参考:http://blog.csdn.net/wujysh/article/details/43085233
ProblemI. International Collegiate Routing Contest

Description

You may know thatBluegao University (formly Bluefly University) is famous ofnetworking technology. One day, their headmaster Yuege received aspecial router, along with a task about routing table.

In this problem,routing table is a (probably) big table with several items, each itemrepresents a subnet. The router has limited function, it can onlydeal with two next-hops and one main routing table. Packets will besend to next hop A if there exists a subnet containing thedestination of the packet in the main routing table. Otherwise theywill be send to next hop B.

You may know that,IPv4 uses 32-bit (four-byte) addresses, which limits the addressspace to 4294967296 (2^32) addresses. IPv4 addresses may be writtenin any notation expressing a 32-bit integer value, for humanconvenience, they are most often written in the dot-decimal notation,which consists of four octets of the address expressed individuallyin decimal and separated by periods. But their binary notation isalso very useful. For example, IP address 128.2.142.23 can beexpressed in dot-binary notation as10000000.00000010.10001110.00010111.

A subnet is a blockof adjacent IP addresses with exactly same binary prefix, and usuallywritten as the first IP address in its address space together withthe bit length of prefix, like “202.120.224.0/24”. If an IPaddress is in the range of an subnet, we say that this subnetcontains the IP address.

Yuege’s task isto invert the behaviour of his router, make all packets currentlyrouted to hop A route to hop B, and vice versa. Also he wants to keepthe size of the main routing table as small as possible, forperformance.

In short, for agiven routing table (i.e. a bunch of subnets), we need to get its“complement”, i.e. calculate a minimum set of subnets which haveno intersection with given subnets, and their union must be the wholeIPv4 address space.

Remember thatBluegao University is famous of networking tech, as headmaster ofBluegao 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 ofthe input gives the number of test cases, T. T test cases follow.

For each test case,the first line contains an integer n (0 ≤ n ≤ 30000), the numberof subnets. Next n lines, each contains a subnet in the format ofa.b.c.d/l, a, b, c, d, l are all integers, 0 ≤ a, b, c, d < 256,0 ≤ l ≤ 32.

Note that even if l= 32, the “/32” part should not be omitted. And if l = 0, the IPaddress 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 nindicates the number of subnets in your answer. Next n lines eachline contains a subnet in the same format as input. You may outputthem in any order.

Samples

Sample Input

Sample Output

3

0

1

0.0.0.0/1

1

128.0.0.0/1

Case #1:

1

0.0.0.0/0

Case #2:

1

128.0.0.0/1

Case #3:

1

0.0.0.0/1

知识点:

Trie树,二进制。

题目大意:

输入IPv4地址空间中的一些子网号,构成一个网络集合。

输出最小的一个网络集合,要求其与输入集合没有交集,且相对IPv4地址空间全集,是输入集合的补集。输出集合包含的子网号,格式遵循网络规范。

解题思路:

每个IP地址由32位二进制组成。整个地址空间可以表现为一棵二叉树(简化的Trie树),

const int Maxn = 30000;int t, n;const int MaxNodes = (Maxn+5) * 33+5;int child[MaxNodes][2], flag[MaxNodes], sz;void Init() {    sz = 1;    memset(child[0], -1, sizeof(child[0]));    memset(flag, 0, sizeof(flag));}void Insert(const char * s, int len) {    int i = 0, idx = 0;    while (idx < len) {        int id = s[idx] - '0';        if (child[i][id] == -1) {            memset(child[sz], -1, sizeof(child[sz]));            child[i][id] = sz++;        }        ++idx;        i = child[i][id];    }    flag[i] = 1;}char ip_buf[32];vector<pair<string, int> > ans;void add_ans(int len) {    //string tmp(ip_buf, 32);    string tmp = "";    char buf[10];    for (int i=0;i<4;++i) {        int val = 0;        for (int j=0;j<8;++j) val = val*2 + ip_buf[i*8+j] - '0';        sprintf(buf, "%d", val);        if (i == 3) tmp += buf;        else {            tmp += buf;            tmp += '.';        }    }    ans.push_back(make_pair(tmp, len-1));}void dfs(int idx, int now) {    //cout << idx << ' ' << now << endl;    if (now == -1) {        add_ans(idx+1);        return;    }    if (flag[now]) return;    if (child[now][0] == -1 && child[now][1] == -1) return;    ip_buf[idx] = '1';    //cout << "go 1\n";    dfs(idx+1, child[now][1]);    ip_buf[idx] = '0';    //cout << "go 0\n";    dfs(idx+1, child[now][0]);}void solve() {    //cout << "debug: \n";    Init();    scanf("%d", &n);    char buf[20];    // 特判    if (!n) {printf("1\n0.0.0.0/0\n");return;}    int a[5];    rep(i, 1, n) {        scanf("%d.%d.%d.%d/%d", &a[0], &a[1], &a[2], &a[3], &a[4]);        if (a[0]+a[1]+a[2]+a[3] == 0 && a[4] == 0) {printf("0\n");return;}        string pre = "";        rep(j, 0, 3) {            // 利用bitset,方便地转换为二进制            bitset<8> foo (a[j]);            pre += foo.to_string();        }        //printf("%s\n", pre.substr(0, a[4]).c_str());        Insert(pre.substr(0, a[4]).c_str(), a[4]);    }    ans.clear();    dfs (0, 0);    int sz = ans.size();    printf("%d\n", sz);    rep(i, 0, sz-1) {        pair<string, int> & p = ans[i];        printf("%s/%d\n", p.first.c_str(), p.second);    }}int main() {#ifndef ONLINE_JUDGE    freopen("input.in", "r", stdin);    //freopen("output.out", "w", stdout);#endif    //SPEED_UP    scanf("%d", &t);    int kase = 0;    rep(i, 0, 31) ip_buf[i] = '0';    while (t--) {        printf("Case #%d:\n", ++kase);        solve();    }    return 0;}
阅读全文
0 0
原创粉丝点击