http://hihocoder.com/problemset/problem/1289

这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决。不过当时还有一个想法就是离线处理,把所有查询进行排序,然后用rule去匹配查询,进行染色处理,而且每个查询只进行一次染色。事实证明,如果比赛的时候采用第二种方法应该能拿全分,但是我用了第一种方法,导致只拿了10分。。。因为我没有考虑同一个rule出现两次的情况,但是字典树中会直接被后面的rule覆盖,所以需要判定更新最小的index,但是离线染色的情况只染一次,所以第二种会直接跳过这种坑点。。

字典树比较好考虑,就是直接存入rule的first mask部分,然后添加一个index和isok标记表示是第几个rule和rule的类型,然后后面的ip一旦匹配的话,只匹配index最小的。然后一些边界及不存在情况进行特判一下。

当时写完字典树WA只拿了10分,然后字典树也没有发现哪里写的有问题,就一直纠结与是不是数据溢出之类的。。唉,也只能说明水平还没到把,也不知道能不能进面试。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; const int maxN = ;
const int maxLen = ;//len表示数的二进制最大长度
struct Trie
{
int index;
int next[];
}tree[maxN*maxLen];
bool isok[maxN*maxLen];
int cnt; void initTree()
{
cnt = ;
memset(tree, -, sizeof(tree));
} void add(int x, bool flag, int index, int len)
{
int now = ;
bool k;
for (int i = len-; i >= ; i--)
{
k = x&(<<i);
if (tree[now].next[k] == -)
tree[now].next[k] = ++cnt;
now = tree[now].next[k];
}
if (tree[now].index == - || tree[now].index > index)//只因为这种情况,只拿了10分。。
{
tree[now].index = index;
isok[now] = flag;
}
} bool query(int x)
{
int v = -, now = ;
bool k, flag;
for (int i = maxLen-; i >= ; i--)
{
if (tree[now].index != -)
{
if (v == - || v > tree[now].index)
{
v = tree[now].index;
flag = isok[now];
}
}
k = x&(<<i);
if (tree[now].next[k] == -) break;
now = tree[now].next[k];
}
if (tree[now].index != -)
{
if (v == - || v > tree[now].index)
{
v = tree[now].index;
flag = isok[now];
}
}
if (v == -) return true;
else return flag;
} int n, m;
char op[];
char str[]; void input()
{
bool flag;
int k, a, b, c, d, r, len;
for (int i = ; i < n; ++i)
{
scanf("%s%s", op, str);
if (op[] == 'a') flag = true;
else flag = false;
r = -;
len = strlen(str);
for (int j = ; j < len; ++j)
{
if (str[j] == '/')
{
sscanf(str+j+, "%d", &r);
str[j] = '\0';
}
}
sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
k = (a<<)+(b<<)+(c<<)+d;
if (r != -) k >>= (-r);
else r = maxLen;
add(k, flag, i, r);
}
} void work()
{
bool flag;
int k, a, b, c, d;
for (int i = ; i < m; ++i)
{
scanf("%d.%d.%d.%d", &a, &b, &c, &d);
k = (a<<)+(b<<)+(c<<)+d;
flag = query(k);
if (flag) printf("YES\n");
else printf("NO\n");
}
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF)
{
initTree();
input();
work();
}
return ;
}

ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))的更多相关文章

  1. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  2. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

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

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

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

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

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

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

  6. hihoCoder 403 Forbidden 字典树

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

  7. ACM学习历程——hihoCoder挑战赛10A 01串(策略)

    时间限制:7000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个整数n和m,求是否存在恰好包含n个0和m个1的01串S,使得S中不存在子串"001"和" ...

  8. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  9. ACM学习历程—Hihocoder 1139 二分·二分答案(bfs)

    http://hihocoder.com/problemset/problem/1139 这题提示上写的是二分,但是感觉不二分应该也可以,至少题目是AC的... 二分的思想就是二分答案的值,看能不能在 ...

随机推荐

  1. RecyclerView的使用(3)之加入Header和Footer

    原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/50742544 李济洲的博客 RecyclerView尽管作为ListView的替 ...

  2. 洛谷P1038 神经网络==codevs1088 神经网络

    P1038 神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神 ...

  3. 【BZOJ2422】Times 树状数组

    [BZOJ2422]Times Description 小y作为一名资深的dotaer,对视野的控制有着深刻的研究.每个单位在一段特定的时间内会出现在小y的视野内,除此之外的时间都在小y看不到的地方. ...

  4. Aeroplane chess(简单概率dp)

    Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz start ...

  5. java实现数字的反转

    例如有一个数字是:19911002,要求是,我要得到它的反转后的数:20011991 实现如下: static void reverse(int a) { int rs = 0; while (a & ...

  6. centos 安装Phpstorm

    下载: http://www.jetbrains.com/phpstorm/download/#section=linux 解压: tar -zxf PhpStorm-8.0.1.tar.gz # 然 ...

  7. 用swift创建各种UI控件【iSwifting社区】

    为了方便大家学习,www.iSwifting.com社区为大家准备了创建各种UI控件的代码.開始看着语法可能有些别扭,当用习惯了,就认为还是非常不错的. 社区还添加了问答专区.有问题的朋友.虽然问.大 ...

  8. rails 命名

    1.model rails g model wordSetting model:WordSetting has_many: word_settings table: word_settings vie ...

  9. [原创]java WEB学习笔记01:javaWeb之tomcat的安装和配置

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  10. 前端基础-CSS属性操作

    前端基础-CSS属性操作 css text 文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB( ...