首页 技术 正文
技术 2022年11月8日
0 收藏 494 点赞 1,702 浏览 3311 个字

传送门

#1289 : 403 Forbidden

时间限制:10000ms单点时限:1000ms内存限制:256MB

描述

Little Hi runs a web server. Sometimes he has to deny access from a certain set of malicious IP addresses while his friends are still allow to access his server. To do this he writes N rules in the configuration file which look like:

allow 1.2.3.4/30
deny 1.1.1.1
allow 127.0.0.1
allow 123.234.12.23/3
deny 0.0.0.0/0

Each rule is in the form: allow | deny address or allow | deny address/mask.

When there comes a request, the rules are checked in sequence until the first match is found. If no rule is matched the request will be allowed. Rule and request are matched if the request address is the same as the rule address or they share the same first mask digits when both written as 32bit binary number.

For example IP “1.2.3.4” matches rule “allow 1.2.3.4” because the addresses are the same. And IP “128.127.8.125” matches rule “deny 128.127.4.100/20” because 10000000011111110000010001100100 (128.127.4.100 as binary number) shares the first 20 (mask) digits with 10000000011111110000100001111101 (128.127.8.125 as binary number).

Now comes M access requests. Given their IP addresses, your task is to find out which ones are allowed and which ones are denied.

输入

Line 1: two integers N and M.

Line 2-N+1: one rule on each line.

Line N+2-N+M+1: one IP address on each line.

All addresses are IPv4 addresses(0.0.0.0 – 255.255.255.255). 0 <= mask <= 32.

For 40% of the data: 1 <= N, M <= 1000.

For 100% of the data: 1 <= N, M <= 100000.

输出

For each request output “YES” or “NO” according to whether it is allowed.

样例输入
5 5
allow 1.2.3.4/30
deny 1.1.1.1
allow 127.0.0.1
allow 123.234.12.23/3
deny 0.0.0.0/0
1.2.3.4
1.2.3.5
1.1.1.1
100.100.100.100
219.142.53.100
样例输出
YES
YES
NO
YES
NO

题解转自:

http://blog.csdn.net/zqh_1991/article/details/51103367

以及

http://paste.ubuntu.net/15664623/  田神的代码

题解:N最大100000,暴力(n^2)算法必然超时。想到用字典树做。IP转化为32位二进制数,需用Long long型变量!注意题中要求返回第一个匹配的状态,每个节点有一个Index记录该节点是第几个匹配ip。注意mask为0的情况。

note:

1)注意题中要求返回第一个匹配的状态,每个节点有一个id[i]记录该节点是第几个匹配ip。

2)对于allow和deny两种状态,不需要建2颗字典树,用end[i]区分allow和deny即可。

3)ip会超int,要用long long

4)可以用位运算,获取ip的每一位数字

1289 403 Forbidden AC G++ 970ms 53MB 2分钟前 查看
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring> int const N = 4e6;
#define ll long long
#define inf 0x3ffffff using namespace std; struct Trie
{
int root,tot;
int next[N][]; //下一个节点编号
int end[N]; //0表示末尾为deny
int id[N]; //第几个ip
int NewNode()
{
memset(next[tot],-,sizeof(next[tot]));
end[tot] = -;id[tot] = -;
return tot++;
}
void ini()
{
memset(id,-,sizeof(id));
tot = ;
root = NewNode();
}
void insert(ll x,int mask,int end_tmp,int id_tmp) //mask,匹配位数
{
int p = root;
ll cnt = 1LL << ;
for(int i = ;i < mask;i++){
int digit;
if( (x & cnt) == cnt ) digit = ;
else digit = ;
if(next[p][digit] == -){
next[p][digit] = NewNode();
}
p = next[p][digit];
cnt>>=;
}
if(id[p] == -){ //要判断,防止被后面的 rule替换了
id[p] = id_tmp;
end[p] = end_tmp;
}
}
int search(ll x)
{
int p = root;
int res_id = inf;
int res_end = -;
ll cnt = 1LL << ;
if(id[p] != -){
res_id = id[p];
res_end = end[p];
}
for(int i = ;i < ;i++){
int digit;
if( (x & cnt) == cnt ) digit = ;
else digit = ;
if(next[p][digit] == -){
break;
}
p = next[p][digit];
if(id[p] != - && id[p] < res_id){ //要判断,选取最小的id
res_id = id[p];
res_end = end[p];
}
cnt>>=;
}
return res_end;
}
}tr; char s[]; int main()
{
int n,m;
//freopen("in.txt","r",stdin);
tr.ini();
int a1, a2, a3, a4;
char ch;
ll x;
int mask;
scanf("%d %d", &n, &m);
int tmp_end;
for(int i = ; i <= n; i++)
{
scanf("%s %d.%d.%d.%d", s, &a1, &a2, &a3, &a4);
mask = ;
if(strcmp(s,"allow")==) tmp_end = ;
else tmp_end = ;
scanf("%c", &ch);
int flag = (ch == '/');
if(flag)
scanf("%d", &mask);
x = (a1 << ) + (a2 << ) + (a3 << ) + a4;
tr.insert(x,mask,tmp_end,i);
}
while(m --)
{
scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4);
x = (a1 << ) + (a2 << ) + (a3 << ) + a4;
printf("%s\n", tr.search(x) ? "YES" : "NO");
} }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293