东大OJ-1588: Routing Table
题目描述
In the computer network, a Router is a device which finds an optimal way to transmit the datagrams passing through it to it's destination efficiently. To accomplish this task, the Router maintains a Routing Table.
The Routing Table stores a variety of relevant data about transmission path. In other words, the information contained in the table determines the forwarding strategy of a datagram.
Normally, the information in the Routing Table for each routing table entry is:
First the destination IP Address, followed by the number of bits of the sub-net mask, and finally the forwarded network port number of the destination network.
For each datagram passing through it, the Router compares the datagram’s destination IP Address with the information of routing table entry, if the network number of the destination IP Address is equals to the network number stored in the routing table entry,
then the datagram is forwarded to the corresponding port.
Now, give you the Routing Table stored in the Router. Then for each datagram travel through this Router, give you it's destination IP Address, please return which network port will the datagram be forwarded to.ou
You will get some help:baike.baidu.com/link
输入
The first line of input contains an integer T, indicating the number of test cases (T ≤ 20).
The first line of each test case contains two integers N and M, represent the number of entries in the Routing Table and the number of datagram passing through the Router, N and M are all less than 100000. Nest N lines each line represent a routing table entry,
the format of input is IP Address/bits of sub-net mask and forwarded port number. And next M lines each line contain a destination IP Address. Please refer to the sample input for more detail.
输出
For each destination IP Address, please output the port number that the Router should forward. If there are many entry both match to this destination IP Address, please output the one that has the longest bits of sub-net mask. If there are also many entry match,
please output the one that has the smallest port number. If there are none entry match, please output the default port 65535.
样例输入
1 4 4 192.168.0.0/16 1234 192.168.1.0/24 1235 192.168.1.0/23 1233 192.168.0.0/23 1236 192.168.2.0 192.168.0.0 192.168.1.0 255.255.255.255
样例输出
1234 1233 1235 65535
第一次在ACM的题中手打哈希表,好高兴,一次成功了.
哈希表实现要领
1.哈希表中每个元素都是指针,指向某元素.
2.很像链式前向星的一种结构
2.很像链式前向星的一种结构
#include<iostream> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 1e5 + 7; const int len = maxn * 10; const int P = 1e9 + 7; struct Node{ int ip, bits,to; int next; }mem[maxn]; int mi; struct Hash{ int a[len]; void init(){ memset(a, 0, sizeof(a)); } void insert(int ip, int bits,int to){ int pos = abs((ip*17)%P+13*bits)%len; for (int i = a[pos]; i; i = mem[i].next){ if (mem[i].ip == ip&&mem[i].bits == bits){ mem[i].to = min(mem[i].to, to); return; } } mem[++mi].ip = ip, mem[mi].bits = bits, mem[mi].to = to, mem[mi].next = a[pos], a[pos] = mi; } int get(int ip, int bits){ int pos = abs((ip * 17) % P + 13 * bits) % len; for (int i = a[pos]; i; i = mem[i].next){ if (mem[i].ip == ip&&mem[i].bits == bits)return mem[i].to; } return -1; } }dic; int mask[33]; void init(){ mask[32] = ~0; for (int i = 31; i >= 0; i--)mask[i] = mask[i + 1] << 1; } int read(){ int a, b, c, d; scanf("%d.%d.%d.%d", &a, &b, &c, &d); int ans = (a << 24) | (b << 16) | (c << 8) | d; return ans; } int main(){ freopen("in.txt", "r", stdin); init(); int T; scanf("%d", &T); while (T--){ int N, M; scanf("%d%d", &N, &M); dic.init(); mi = 0; while (N--){ int bits,to; int ip = read(); scanf("/%d%d", &bits,&to); ip &= mask[bits]; dic.insert(ip, bits, to); } while (M--){ int ip = read(); for (int i = 32; i >= 0; i--){ ip&=mask[i]; int ans = dic.get(ip, i); if (ans ^-1){ printf("%d\n", ans); goto over; } } printf("65535\n"); over:; } } return 0; }
东大OJ-1588: Routing Table的更多相关文章
- 东大OJ 2SAT 异或
看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...
- 理解 OpenStack 高可用(HA)(3):Neutron 分布式虚拟路由(Neutron Distributed Virtual Routing)
本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...
- A Quick Introduction to Linux Policy Routing
A Quick Introduction to Linux Policy Routing 29 May 2013 In this post, I’m going to introduce you to ...
- 东大OJ-Max Area
1034: Max Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 40 解决: 6 [提交][状态][讨论版] 题目描述 又是这道题,请不要惊讶,也许你已经见过了,那就请你再 ...
- Routing in ASP.NET Web API和配置文件的设定读取
Routing Tables In ASP.NET Web API, a controller is a class that handles HTTP requests. The public me ...
- ASP.NET Web API中的Routing(路由)
[译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...
- Study之6 Neutron(配置使用 Routing)-devstack
●Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 firewall 和 floating ip 服务. l3 agent 需要正确 ...
- Routing Manager for WCF4 z
http://www.codeproject.com/Articles/77198/Routing-Manager-for-WCF Download source Contents Features ...
- Classless Interdomain Routing (CIDR)
IP Address Problems IP Address Exhaustion Class A, B, and C address structure inefficient Class B to ...
随机推荐
- C++杂谈(二)初识vector容器与迭代器
教科书中失踪的vector 很奇怪的一件事情,在当时学习C++的时候,老师并没有讲授容器的内容,当时参考的谭浩强老师的红皮C++也没有这个内容,不知为何.后来再学C++,发现容器是一个很重要的概念,在 ...
- Android中 int,float,Double,String 互相转换
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt( ...
- windows文件关联、打开方式列表之修改注册表攻略
这里全是修改注册表的方式.网上找了半天,有的仅有添加文件关联的方法,却没有添加到打开方式列表里面的方法:有的有添加到文件列表的方法,却是使 用控制面板->文件夹选项的.好难得才找齐所有,从添加文 ...
- js判断手机访问PC端跳转到手机站
<script type="text/javascript">(function() { //得到域名后缀 var path = location.pathname.s ...
- Medusa引擎开源了
首先贴出 Github地址 然后博客地址 相比于市面上其他的著名游戏引擎,例如Unity,cocos2dx,Unreal,Medusa游戏引擎目前还非常的简陋,目前实现的功能还集中在2D部分,3D的虽 ...
- 【原】常见CSS3属性对ios&android&winphone的支持
2个月前,我在博文<webapp开发中兼容Android4.0以下版本的css hack>中写过“那对于做移动网页开发的同事来说,一般只要做好webkit内核浏览器的展现效果就行了” ,在 ...
- cdoj 1489 老司机采花
地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others) M ...
- 翻译《Writing Idiomatic Python》(五):类、上下文管理器、生成器
原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...
- SpringMVC学习系列-后记 解决GET请求时中文乱码的问题
SpringMVC学习系列-后记 解决GET请求时中文乱码的问题 之前项目中的web.xml中的编码设置: <filter> <filter-name>CharacterEnc ...
- android整体架构概述--①
android的logo 是由设计师去厕所时来的灵感. 其中android的命名都是以甜点的名字来定的. android的系统一共有四层. 1.Linux内核和驱动层 2.函数库 由C或C++编写 ...