ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
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(字典树 || (离线 && 排序 && 染色))的更多相关文章
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)
传送门 #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Someti ...
- hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)
#1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...
- 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...
- hihoCoder 403 Forbidden 字典树
题意:给定个规则,个ip,问这些ip是否能和某个规则匹配,如果有多个规则,则匹配第一个.如果没能匹配成功,则认为是"allow",否则根据规则决定是"allow" ...
- ACM学习历程——hihoCoder挑战赛10A 01串(策略)
时间限制:7000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个整数n和m,求是否存在恰好包含n个0和m个1的01串S,使得S中不存在子串"001"和" ...
- ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)
http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...
- ACM学习历程—Hihocoder 1139 二分·二分答案(bfs)
http://hihocoder.com/problemset/problem/1139 这题提示上写的是二分,但是感觉不二分应该也可以,至少题目是AC的... 二分的思想就是二分答案的值,看能不能在 ...
随机推荐
- php之 人员的权限管理
1.想好权限管理的作用? 2.有什么权限内容? 3.既然有权限管理那么就会有管理员? 4.登录后每个人员的界面会是不一样的? 一.想好这个权限是什么? 就做一个就像是vip的功能,普通用户和vip用户 ...
- Ajax 中的高级请求和响应
一.概述 在本文中,重点介绍这个请求对象的 3 个关键部分的内容: 1.HTTP 就绪状态 2.HTTP 状态代码 3.可以生成的请求类型 这三部分内容都是在构造一个请求时所要考虑的因素:但是介绍这些 ...
- [IOI2018]组合动作
IOI2018 组合动作 UOJ 首先显然可以两次试出首字母 考虑增量构造 假设首字母为A,且已经试出前i个字母得到的串s 我们考虑press这样一个串s+BB+s+BX+s+BY+s+XA 首先这个 ...
- redis持久化RDB详细操作步骤
1.xshell远程登录服务器ssh root@192.168.142.130 2.切换到redis目录 3.创建一个配置文件s2-redis.conf 4.编辑文件 vi s2-redis.conf ...
- READ_TEXT
[转自http://lz357502668.blog.163.com/blog/static/1649674320109119101907/]这里,定义ITAB内表来存储长文本,并放到内表ITAB_E ...
- C#窗体互动
说白了就是在一个窗体操作另外一个窗体的东西. 原理是把form2的数据提取出来,利用中间的静态类middle来传递数据,触发事件,调用委托,来修正form1 效果如下: Form1.cs usin ...
- LeetCode:删除排序链表中的重复元素【83】
LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...
- Java多线程系列 面试题
1. https://blog.csdn.net/jjj19891128/article/details/24393661 多线程经典面试题 2. https://blog.csdn.net/ll6 ...
- 【leetcode刷题笔记】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- nginx日志配置,以及日志轮询
一.为nginx配置错误日志 Nginx错误日志是调试nginx的重要手段,属于核心功能模块的参数(ngx_core_module)该参数名字为err_log,是放在Main区块中全局配置 err_l ...