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 with10000000011111110000100001111101 (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
题解:N最大100000,暴力(n^2)算法必然超时。想到用字典树做。IP转化为32位二进制数,需用Long long型变量!注意题中要求返回第一个匹配的状态,每个节点有一个Index记录该节点是第几个匹配ip。注意mask为0的情况。
#include<iostream>
#include<cstdio>
#include<string>
#include<set>
#include<map>
#include<cstring>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<sstream>
#include<limits>
using namespace std;
struct TrieNode{
TrieNode* next[2];
int index;
bool flag;
bool end;
TrieNode() {
memset(next,0,sizeof(next));
flag=false;
index=-1; //是第几个ip
end=false; //终点标志
}
};
TrieNode* root;
int res_index;
bool res_f;
int nonExsit; //针对都没有匹配上的情况。如果有mask=0的情况,则有可能是true,有可能是false。这里nonExsit=-1则说明没有mask=0的情况。
void insert(long long tmp,int mask,bool f,int tmp_index) { //nonExsit=1表示allow,nonExsit=-1代表deny
long long cnt=(long long)1<<31; //要用Long long转换!
TrieNode* p=root;
for(int i=0;i<mask;i++) {
int num;
if((tmp&cnt)==cnt) num=1;
else num=0;
if(p->next[num]==NULL) {
p->next[num]=new TrieNode();
}
p=p->next[num];
cnt>>=1;
}
if(p->index==-1) {
p->index=tmp_index;
p->flag=f;
}
p->end=true;
}
bool search(long long tmp) {
TrieNode* p=root;
long long cnt=(long long)1<<31;
for(int i=0;i<32;i++) {
int num;
if((tmp&cnt)==cnt) num=1; //每一位是0还是1
else num=0;
if(p->next[num]==NULL) {
break;
}
if(p->next[num]->end) {
if(res_index==-1||p->next[num]->index<res_index) { //取最小的Index状态,代表最先匹配的
res_index=p->next[num]->index;
res_f=p->next[num]->flag;
}
}
p=p->next[num];
cnt>>=1;
}
if(res_index==-1) {
if(nonExsit!=0)
return true;
else return false;
}
else return res_f;
}
int main() {
int n,m;
scanf("%d%d",&n,&m);
nonExsit=-1;
char ss[20];
char ip[101];
root=new TrieNode();
for(int i=0;i<n;i++) {
scanf("%s",ss);
long long a,b,c,d;
scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
char cc=getchar();
int mask=32;
if(cc=='/') scanf("%d",&mask);
if(mask==0) {
if(nonExsit==-1) {
if(strcmp(ss,"allow")==0) nonExsit=1;
else nonExsit=0;
}
}
long long tmp=(a<<24)|(b<<16)|(c<<8)|d;
if(strcmp(ss,"allow")==0) {
insert(tmp,mask,true,i);
}
else {
insert(tmp,mask,false,i);
}
}
for(int i=0;i<m;i++) {
long long a,b,c,d;
scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
long long tmp=(a<<24)|(b<<16)|(c<<8)|d;
res_index=-1;
bool f=search(tmp);
if(f) printf("YES\n");
else printf("NO\n");
}
return 0;
}