HDU 4760 – Good Firewall [Trie]

Link

传送门

Problem

Professor X is an expert in network security. These days, X is planning to build a powerful network firewall, which is called Good Firewall (a.k.a., GFW). Network flows enter in the GFW will be forwarded or dropped according to pre-established forwarding policies.

Basically, a forwarding policy P is a list of IP subnets, {ip_subnet_1, …, ip_subnet_n}. If P is enabled in GFW, a network flow F with source and destination IP address both located in P can be accepted and forwarded by GFW, otherwise F will be dropped by GFW.

You may know that, an IP address is a 32-bit identifier in the Internet, and can be written as four 0~255 decimals. For example, IP address 01111011.00101101.00000110.01001110 can be expressed as 123.45.6.78. An IP subnet is a block of adjacent IP address with the same binary prefix, and can be written as the first IP address in its address block together with the length of common bit prefix. For example, IP subnet 01111011.00101101.00000100.00000000/22 (123.45.4.0/22) is an IP subnet containing 1024 IP addresses, starting from 123.45.4.0 to 123.45.7.255. If an IP address is in the range of an IP subnet, we say that the IP address is located in the IP subnet. And if an IP address is located in any IP subnet(s) in a policy P, we say that the IP address is located in the policy P.

How will you design the GFW, if you take charge of the plan?

The input file contains no more than 32768 lines. Each line is in one of the following three formats:

E id n ip_subnet_1 ip_subnet_2 … ip_subnet_n
D id
F ip_src ip_dst

The first line means that a network policy P id (1<=id<=1024) is enabled in GFW, and there are n (1<=n <=15) IP subnets in P id. The second line means that policy P id (which is already enabled at least once) is disabled in GFW. The last line means that a network flow with source and destination IP address is entered in GFW, and you need to figure out whether GFW is going to forward (F) or drop (D) this flow:

1. If the source and destination IP address both are located in one of enabled policy group P id, GFW will forward this flow.

2. Otherwise GFW will drop this flow. That is, if the source or destination IP address is not located in any of enabled policy group, or they are only located in different enabled policy group(s), GFW will drop it.

IP subnets can be overlapped. An IP address may or may not be located in any policy group, and can also be located in multiple policy groups.

In the global routing table, most of the IP subnets have at least 2^8 IP addresses, and at most 2^24 IP addresses. In our dataset, every IP subnet has a prefix length between 8 and 24.

For each ‘F’ operation, output a single ‘F’ (forward) or ‘D’ (drop) in a single line. Just see the sample output for more detail.

Mean

给你一些ip子网,多个ip子网可共有一个id,可以在某个时刻关闭某id的所有ip子网。现有多次查询某起始终止ip,问是否在同一id内。

Analysis

一般这种ip的题都是用字典树,插入查询之类的快。这道题也是。

需要注意的是多个子网可以重合,所以Trie中以及查询时都要加set,判断两个地址是否在同一id中也用set求一下交集。

Code

#include <bits/stdc++.h>
using namespace std;

char op[4];
int a[4], ip[40], id, n, port;
bool cancle[1100];

struct Trie {
    static const int maxnode = 400000, sigma_size = 2;
    int ch[maxnode][sigma_size], sz;
    set<int> st[maxnode];

    Trie() { clear(); }
    void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); st[0].clear(); }

    void insert(const int *ip, int pid, int len) {
        int u = 0;
        for(int i = 0; i < len; ++i) {
            int id = ip[i];
            if(!ch[u][id]) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                st[sz].clear();
                ch[u][id] = sz++;
            }
            u = ch[u][id];
        }
        st[u].insert(pid);
    }

    void query(const int *ip, set<int> &ans) {
        int u = 0;
        for(int i = 0; i < 32; ++i) {
            int id = ip[i];
            if(!ch[u][id]) return;
            else {
                u = ch[u][id];
                for(auto j : st[u])
                    if(!cancle[j])
                        ans.insert(j);
            }
        }
    }
}trie;

void encode(int a[4]) {
    for(int i = 0; i < 4; ++i)
        for(int j = ((i + 1) * 8) - 1; j >= i * 8; --j) {
            ip[j] = a[i] % 2;
            a[i] /= 2;
        }
}

int main() {
    trie.clear();
    while(~scanf("%s", op)) {
        if(op[0] == 'E') {
            scanf("%d%d", &id, &n);
            for(int i = 0; i < n; ++i) {
                scanf("%d.%d.%d.%d/%d", &a[0], &a[1], &a[2], &a[3], &port);
                encode(a);
                trie.insert(ip, id, port);
            }
            cancle[id] = false;
        }
        else if(op[0] == 'D') {
            scanf("%d", &id);
            cancle[id] = true;
        }
        else if(op[0] == 'F') {
            set<int> st[2];
            for(int i = 0; i < 2; ++i) {
                scanf("%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
                encode(a);
                trie.query(ip, st[i]);
            }
            vector<int> ans;
            set_intersection(st[0].begin(), st[0].end(), st[1].begin(), st[1].end(), back_inserter(ans));
            puts(ans.empty() ? "D" : "F");
        }
    }
    return 0;
}

欢迎留言