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. 【BZOJ3744】Gty的妹子序列 分块+树状数组

    [BZOJ3744]Gty的妹子序列 Description 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzo ...

  2. 【BZOJ2741】【FOTILE模拟赛】L 分块+可持久化Trie树

    [BZOJ2741][FOTILE模拟赛]L Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max( ...

  3. polynomial time

    https://en.wikipedia.org/wiki/Time_complexity#Polynomial_time An algorithm is said to be of polynomi ...

  4. Codeforces441C_Valera and Tubes(暴力)

    Valera and Tubes time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. ACM-最小生成树之继续畅通project——hdu1879

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/lx417147512/article/details/27092583 ************** ...

  6. host更新

    http://alsohosts.herokuapp.com/ google镜像站https://goge.ml/

  7. SQL的优化1

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  8. python3 批量缩放图片为iphone5的640*1136以下

    try: from PIL import Image, ImageDraw, ImageFont, ImageEnhance except ImportError: import Image, Ima ...

  9. SpringBoot学习笔记(4):与前端交互的日期格式

    SpringBoot学习笔记(4):与前端交互的日期格式 后端模型Date字段解析String 我们从前端传回来表单的数据,当涉及时间.日期等值时,后端的模型需将其转换为对应的Date类型等. 我们可 ...

  10. ButterKnife 原理解析

    一.使用方法 1.添加依赖. implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewhart ...