Good Firewall(字典树 HDU4760)

时间:2022-09-13 12:51:21

Good Firewall

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 713 Accepted Submission(s): 203

Problem Description

Professor X is an expert in network security. These days, X is planning to build a powerful network firewall, which is called Good Firewall (a.k.a., GFW). Network flows enter in the GFW will be forwarded or dropped according to pre-established forwarding policies.

Basically, a forwarding policy P is a list of IP subnets, {ip_subnet_1, …, ip_subnet_n}. If P is enabled in GFW, a network flow F with source and destination IP address both located in P can be accepted and forwarded by GFW, otherwise F will be dropped by GFW.

You may know that, an IP address is a 32-bit identifier in the Internet, and can be written as four 0~255 decimals. For example, IP address 01111011.00101101.00000110.01001110 can be expressed as 123.45.6.78. An IP subnet is a block of adjacent IP address with the same binary prefix, and can be written as the first IP address in its address block together with the length of common bit prefix. For example, IP subnet 01111011.00101101.00000100.00000000/22 (123.45.4.0/22) is an IP subnet containing 1024 IP addresses, starting from 123.45.4.0 to 123.45.7.255. If an IP address is in the range of an IP subnet, we say that the IP address is located in the IP subnet. And if an IP address is located in any IP subnet(s) in a policy P, we say that the IP address is located in the policy P.

How will you design the GFW, if you take charge of the plan?

Input

The input file contains no more than 32768 lines. Each line is in one of the following three formats:

E id n ip_subnet_1 ip_subnet_2 … ip_subnet_n

D id

F ip_src ip_dst

The first line means that a network policy Pid (1<=id<=1024) is enabled in GFW, and there are n (1<=n <=15) IP subnets in Pid. The second line means that policy Pid (which is already enabled at least once) is disabled in GFW. The last line means that a network flow with source and destination IP address is entered in GFW, and you need to figure out whether GFW is going to forward (F) or drop (D) this flow:

  1. If the source and destination IP address both are located in one of enabled policy group Pid, GFW will forward this flow.

  2. Otherwise GFW will drop this flow. That is, if the source or destination IP address is not located in any of enabled policy group, or they are only located in different enabled policy group(s), GFW will drop it.

IP subnets can be overlapped. An IP address may or may not be located in any policy group, and can also be located in multiple policy groups.

In the global routing table, most of the IP subnets have at least 2^8 IP addresses, and at most 2^24 IP addresses. In our dataset, every IP subnet has a prefix length between 8 and 24.

Output

For each ‘F’ operation, output a single ‘F’ (forward) or ‘D’ (drop) in a single line. Just see the sample output for more detail.

Sample Input

E 1 2 123.45.4.0/22 123.45.8.0/22

F 123.45.4.1 123.45.8.1

F 123.45.8.1 123.45.4.1

E 2 1 123.45.6.0/24

D 1

F 123.45.6.123 123.45.6.234

F 123.45.8.1 123.45.4.1

Sample Output

F

F

F

D

字典树的好题

题目大意:有一个防火墙,具有添加一个子网络,删除一个子网络,以及转发包的操作。

添加操作包含子网络的id,以及子网络的子网掩码(计算出网络前缀,以及ip的下限),不会超过15个。

删除则是给定要删除的子网络id。

转发操作,给定两个ip,如果两个ip在同一个子网络中,则可以转发,否则丢弃。

解题思路:对子网掩码前缀建立字典树,每个前缀终止节点用一个set记录属于哪些子网络,ip下限。那么增加和删除操作既可以解决了。对于查询操作,分别查询两个ip,处理除两个ip可能属于的网络,判断有无共同即可。

#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL; struct Trie
{
int next[2];
vector<int>vec;
} st[1024*15*32+1000]; int top; struct IP
{
int n;
LL res[20];
int len[20];
} sub[1200]; LL bite[40]; char str[110]; int Creat()
{
memset(st[top].next,-1,sizeof(st[top].next));
st[top].vec.resize(0);
return top++;
} void Insert(int Root,int len,int id, LL res)
{
for(int i=1; i<=len; i++)
{
int ans=(bite[32-i]&res)?1:0; if(st[Root].next[ans]==-1)
{
st[Root].next[ans]=Creat();
} Root=st[Root].next[ans];
} st[Root].vec.push_back(id);
} void Delete(int id,int len,LL res,int Root)
{
for(int i=1; i<=len; i++)
{
Root=st[Root].next[(res&bite[32-i])?1:0];
}
for(vector<int>::iterator it=st[Root].vec.begin(); it!=st[Root].vec.end(); it++)
{
if(*it==id)
{
*it=0; break;
}
}
} bool vis[1050]; bool Query(LL res,int num,int Root)
{
vector<int>::iterator it;
for(int i=1; i<=32; i++)
{
for(it = st[Root].vec.begin(); it!=st[Root].vec.end(); it++)
{
if(*it == 0)
{
continue;
}
if(num==1)
{
vis[*it] = true;
}
else
{
if(vis[*it])
{
return true;
}
}
} int ans=(bite[32-i]&res)?1:0; if(st[Root].next[ans]==-1)
{
return false;
} Root = st[Root].next[ans];
}
for(it=st[Root].vec.begin(); it != st[Root].vec.end(); it++)
{
if(*it==0)
{
continue;
} if(num==1)
{
vis[*it]=true;
}
else
{
if(vis[*it])
{
return true;
}
}
}
return false;
} int main()
{
top=0; int a,b,c,d,len; int Root = Creat(); char s[3]; int id,n; bite[0]=1; for(int i=1; i<=40; i++)
{
bite[i]=bite[i-1]<<1;
} while(~scanf("%s",s))
{
if(s[0]=='E')
{
scanf("%d %d",&id,&n); sub[id].n=n; for(int i=1; i<=n; i++)
{
scanf("%s",str); sscanf(str,"%d.%d.%d.%d/%d",&a,&b,&c,&d,&len); LL res = a*256LL*256LL*256LL+b*256LL*256LL+c*256LL+d; sub[id].res[i]=res; sub[id].len[i]=len; Insert(Root,len,id,res);
} }
else if(s[0]=='D')
{
scanf("%d",&id); for(int i=1; i<=sub[id].n; i++)
{
Delete(id,sub[id].len[i],sub[id].res[i],Root);
}
} else if(s[0]=='F')
{
scanf("%s",str); sscanf(str,"%d.%d.%d.%d",&a,&b,&c,&d); memset(vis,false,sizeof(vis)); Query(a*256LL*256LL*256LL+b*256LL*256LL+c*256LL+d,1,Root); scanf("%s",str); sscanf(str,"%d.%d.%d.%d",&a,&b,&c,&d); if(Query(a*256LL*256LL*256LL+b*256LL*256LL+c*256LL+d,2,Root))
{
printf("F\n");
}
else
{
printf("D\n");
}
}
}
return 0;
}

Good Firewall(字典树 HDU4760)的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. &lbrack;LeetCode&rsqb; Implement Trie &lpar;Prefix Tree&rpar; 实现字典树&lpar;前缀树&rpar;

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树&plus;博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C&plus;&plus;里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. &ast;HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. &lbrack;AR&rsqb;Vumark&lpar;下一代条形码&rpar;

    VuMark 准备知识 Vumark的说明: https://library.vuforia.com/articles/Training/VuMark https://library.vuforia. ...

  2. 矢量图绘制工具Svg-edit调整画布的大小

    矢量图绘制工具Svg-edit调整画布的大小 ------------------------------ ------------------------

  3. System&period;Configuration引用后ConfigurationManager方法用不了

    System.Configuration引用后ConfigurationManager方法却用不了,提示没有引用 需手动添加引用 项目-引用-右击-添加引用-找到System.Configuratio ...

  4. pay-as-you-go

    What is pay as you go? A pay as you go deal means you aren’t tied into a contract and can top up you ...

  5. 数组拷贝 copyOf&lpar;&rpar;

    Arrarys类的copyof方法与copyOfRange方法可以实现对数组的复制,前者是复制数组到指定的长度,后者将指定的长度复制到一个新数组中. 1.copyOf()方法 该方法提供了很多种重载形 ...

  6. Servlet基础知识(一)——Servlet的本质

    什么是Servlet: Servlet是运行在web服务器端(web容器,如tomcat)的程序,它与Applet相对,Applet是运行在客户端的程序. Servlet的主要作用是处理客户端的请求, ...

  7. Python 获取 网易云音乐热门评论

    最近在研究文本挖掘相关的内容,所谓巧妇难为无米之炊,要想进行文本分析,首先得到有文本吧.获取文本的方式有很多,比如从网上下载现成的文本文档,或者通过第三方提供的API进行获取数据.但是有的时候我们想要 ...

  8. bzoj 2599

    还是点对之间的问题,果断上点分治 同样,把一条路径拆分成经过根节点的两条路径,对不经过根节点的路径递归处理 然后,我们逐个枚举根节点的子树,计算出子树中某一点到根节点的距离,然后在之前已经处理过的点中 ...

  9. maven入门安装及HelloWorld实现

    一.安装maven 1.下载    https://maven.apache.org/download.cgi     官网进行下载 2.安装 2.1  解压 本人在D盘建立一个maven文件夹,然后 ...

  10. 【leetcode 简单】 第一百零六题 压缩字符串

    给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回数组的新长度. 进阶: ...