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. DICOM:C-GET服务

    背景: 之前博文对照过多次C-MOVE与C-GET服务的差别,两者最大的差别在于C-GET是基于单个TCP连接的点对点的双方服务.而C-MOVE是基于两个TCP连接的三方服务(详情參见:<DIC ...

  2. idangerous swiper

    最近使用Swipe.js,发现中文的资料很少,试着翻译了一下.能力有限,翻译难免错漏,欢迎指出,多谢! 翻译自:http://www.idangero.us/sliders/swiper/api.ph ...

  3. TensorFlow_action

    安装TensorFlow  包依赖 C:\Users\sas> pip3 install --upgrade tensorflow Collecting tensorflow Downloadi ...

  4. 梯度下降算法(gradient descent)

    简述 梯度下降法又被称为最速下降法(Steepest descend method),其理论基础是梯度的概念.梯度与方向导数的关系为:梯度的方向与取得最大方向导数值的方向一致,而梯度的模就是函数在该点 ...

  5. SM30 表格维护生成器

    1)SE11创建自建表,结构如下: 2) 创建表维护 3) 针对上面创建的函数组ZMM_MAT_DESC,做以下增强处理 添加的Module 代码如下: module mod_customize in ...

  6. Django模型系统——ORM表结构对应关系

    对于数据库来说一般表结构只会有三种对应关系,分别是一对一.一对多和多对一,下面分别介绍: 1.一对多 何为一对多,例如一个学生只可能有一个班级,一个班级却又多个学生,班级表和学生表就是一对多的关系. ...

  7. ubuntu切换到root

    sudo+命令,输入当前用户密码后以root权限执行命令,有时间限制且仅限当前命令. sudo -i,输入当前用户密码后以root权限登录shell,无时间限制.使用exit或logout退出. su ...

  8. Spring声明式事务管理与配置介绍

    转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...

  9. Ionic background地址写法问题

    1.背景图片 background:url(‘/img/text.jpg') 这种写法在手机上不好使 ’../img/text.jpg' 这种在手机上好使

  10. HDU - 5703 Desert 【找规律】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5703 题意 给出一杯容量为N的水 每次至少喝1个单位 有多少种不同的方式喝完 比如 给出3 就有4种方 ...