题意:给定个规则,个ip,问这些ip是否能和某个规则匹配,如果有多个规则,则匹配第一个。如果没能匹配成功,则认为是”allow”,否则根据规则决定是”allow”或者”deny”.


思路:字典树,将所有ip全部转换为01串,在字典树上面插入查找。

在求二进制时,注意补前缀0。

坑点:

1.字典树上每一个规则都应该带一个序号,表示这是第几个规则,因为题目要求第一个匹配,那么当你利用ip去寻找匹配的规则时,应该是找到序号最小的。

2.出现deny 0.0.0.0/0,说明所有无法匹配ip的都是deny,因为这些ip和这条规则匹配。

给大家贴几个测试数据:

5 2

deny 0.0.0.0/0

allow 0.0.0.0/0

deny 0.0.0.0/1

deny 0.0.0.0/2

allow 123.234.12.23/3

123.234.12.23

0.234.12.23

答案: NO NO

2 1

deny 1.1.1.1

allow 127.0.0.1

1.1.1.222

答案:YES

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e5 + 5;
bool is_zero, tag;
struct node{
    int ok, order;
    node *nex[2];
    node() {
        ok = -1; //从未访问过
        nex[0] = nex[1] = NULL;
    }
}*root;
void init() {
    root = new node();
}

void insert(string &s, int n, int ok, int order) {
    node *p = root, *q;
    for(int i = 0; i < n; ++i) {
        int u = s[i] - '0';
        if(p->nex[u] == NULL) {
            q = new node();
            p->nex[u] = q;
        }
        p = p->nex[u];
        if(i == n-1 && p->ok == -1) {
            p->ok = ok;
            p->order = order;
        }
    }
}

bool search(string &s) {
    node *p = root;
    bool ok = true;
    int order = inf;
    for(int i = 0; i < s.size(); ++i) {
        int u = s[i]-'0';
        if(p->nex[u] == NULL) break;
        else {
            if(p->nex[u]->ok != -1 && p->nex[u]->order < order) {
                order = p->nex[u]->order;
                ok = p->nex[u]->ok;
            }
        }
        p = p->nex[u];
    }
    //printf("%d\n", order);
    if(is_zero && order == inf) return tag;
    return ok;
}

string get_binary(int x) {
    stack<int>sta;
    do{
        sta.push(x%2);
        x /= 2;
    }while(x);
    string ans = "";
    //补前缀0
    for(int i = 0; i < 8 - sta.size(); ++i) {
        ans += '0';
    }
    //得到二进制
    while(!sta.empty()) {
        ans += sta.top() + '0';
        sta.pop();
    }
    return ans;
}

void deal(char *s, int ok, int order) {
    int len = strlen(s);
    int ind = -1;
    for(int i = 0; i < len; ++i) {
        if(s[i] == '/') {
            ind = i;
            break;
        }
    }
    string ip = "";
    if(ind == -1) ind = len;
    for(int i = 0; i < ind;) {
        if(s[i] >= '0' && s[i] <= '9') {
            int num = 0;
            while(i < ind && s[i] >= '0' && s[i] <= '9'){
                num = num * 10 + (s[i] - '0');
                ++i;
            }
            ip += get_binary(num);
        }
        else ++i;
    }
    int n = 32;
    if(ind != -1 && ind != len) {
        int num = 0;
        for(int i = ind+1; i < len; ++i) {
            num = num * 10 + s[i] -'0';
        }
        if(num == 0 && !is_zero) {
            is_zero = 1;
            tag = ok;
        }
        n = num;
    }
    insert(ip, n, ok, order);
}
int main() {
    init();
    char kind[10], ip[40];
    int n, m;
    while(scanf("%d%d", &n, &m) == 2) {
        is_zero = 0;
        for(int i = 0; i < n; ++i) {
            scanf("%s %s", kind, ip);
            int ok = 0;
            if(kind[0] == 'a') ok = 1;
            deal(ip, ok, i);
        }
        for(int i = 0; i < m; ++i) {
            scanf("%s", ip);
            int x1, x2, x3, x4;
            sscanf(ip, "%d.%d.%d.%d", &x1, &x2, &x3, &x4);
            string res = get_binary(x1) + get_binary(x2) + get_binary(x3) + get_binary(x4);
            if(search(res)) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
} 

如有不当之处欢迎指出!

hihoCoder 403 Forbidden 字典树的更多相关文章

  1. ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))

    http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...

  2. [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)

    传送门 #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Someti ...

  3. hihoCoder 1014trie树(字典树)

    hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...

  4. 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...

  5. 逆序单词 HIhoCoder 1366 字典树

    逆序单词 HIhoCoder 1366 字典序 题意 在英文中有很多逆序的单词,比如dog和god,evil和live等等. 现在给出一份包含N个单词的单词表,其中每个单词只出现一次,请你找出其中有多 ...

  6. hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)

    #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...

  7. Trie树(字典树)

    传送门:http://hihocoder.com/problemset/problem/1014 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...

  8. HihoCoder1656 : 前缀后缀查询([Offer收割]编程练习赛39)(字典树+小技巧)

    描述 给定一个包含N个单词的字典:{W1, W2, W3, ... WN},其中第i个单词Wi有具有一个权值Vi. 现在小Hi要进行M次查询,每次查询包含一个前缀字符串Pi和一个后缀字符串Si.他希望 ...

  9. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

随机推荐

  1. 如何导入css和js?

    导入css <link rel="stylesheet" href="css路径" type="text/css"> 导入js, ...

  2. 使用非java代码编程

    使用非JAVA代码     JAVA语言及其标准API(应用程序编程接口)应付应用程序的编写已绰绰有余.但在某些情况下,还是必须使用非JAVA编码.例如,我们有时要访问操作系统的专用特性,与特殊的硬件 ...

  3. 【转】awk 里的substr函数用法举例

    awk 里的substr函数用法举例: 要截取的内容:2007-08-04 04:45:03.084 - SuccessfulTradeResult(status: 1, currencyPair: ...

  4. 面向对象_03【关键字:final使用】

    final关键字:可修饰类.变量名和方法1,final修饰的类不能被继承2,final修饰的变量(成员.局部)是常量,只能赋值一次.3,final修饰的方法不能被子类重写Example:一:修饰类 / ...

  5. C++ concurrency in action 读随记1

    翻了翻,感觉标准库支持的并发应该是kernel level 的(书里也没有明确写,不过他写了诸如"操作系统来安排""需要知道硬件支持多少线程"等等话语,所以猜测 ...

  6. xBIM WeXplorer

    目录 基础 xBIM WeXplorer 简要介绍 xBIM WeXplorer xViewer 基本应用 xBIM WeXplorer xViewer 浏览器检查 xBIM WeXplorer xV ...

  7. ElasticSearch安装中遇到的一些问题

    前段时间部署ElasticSearch,现把安装中遇到的一些问题和注意细节与大家分享一下. 系统:CentOS7.2 0.安装JDK 8,配置环境变量 官网下载地址:http://www.oracle ...

  8. xftp上传失败之解决办法

    修改/usr/local 文件夹权限 rwx 为不可读不可写第三方不可访问 报错 传输状态 恢复文件夹/usr/local 读写第三方访问权限 成功上传

  9. 机器学习实践之Logistic回归

        关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2017年12月17日 19:18:31所撰写内容(http://blog.cs ...

  10. zabbix安装笔记

    部署监控安装zabbix时,安装环境选择位mysql5.6.29 php选5.4.45 环境安装:centOS7: yum install httpd php mariadb-server mysql ...