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的... 二分的思想就是二分答案的值,看能不能在 ...
随机推荐
- [Android Studio 权威教程]AS 中配置强大的版本号管理系统(Git、SVN、等)
在Eclipse中加入Git等版本号管理工具须要自己加入插件.并且个人认为不咋好用,在AS中已经给我们集成好了,我们仅仅须要配置一下就OK了.今天就和大家聊聊怎么配置以及使用的要点. 1. 安装Git ...
- mysql慢查询日志分析工具(python写的)
D:\NormalSoftware>python mysql_filter_slow_log.py ./mysql1-slow.log --no-duplicates --sort-avg-qu ...
- (转)linux设备驱动之USB数据传输分析 二
3.2:控制传输过程1:root hub的控制传输在前面看到,对于root hub的情况,流程会转入rh_urb_enqueue().代码如下:static int rh_urb_enqueue (s ...
- 看了就很快学会jQuery
一.jQuery简介与第一个jQuery程序 1.1.jQuery简介 1.2.jQuery特点 1.3.jQuery版本 1.4.获得jQuery库 1.5.第一个jQuery程序 二.jQuery ...
- [JavaScript] Imitate String.Format() in c#
Definition if (!String.prototype.format) { String.prototype.format = function () { var args = argume ...
- web 文件下载
response.reset(); response.setContentType("octets/stream"); response.addHeader("Conte ...
- Hibernate 表连接hql语句
现有两个表 user 表 和 VIPcard 表 UserVo user VIPcardVo 中含有 UserVo user select v from VIPCardVo v left join ...
- 1.BeanFactory解析
package org.springframework.beans.factory; import org.springframework.beans.BeansException; import o ...
- PostMan的使用注意事项
1json格式要设置头尾application/json 2body中raw的{"userName":"123","passWord":&q ...
- 016-Spring Boot JDBC
一.数据源装配 通过查看代码可知,默认已装配了数据源和JdbcTemplate System.out.println(context.getBean(DataSource.class)); Syste ...