东大OJ-1588: Routing Table
来源:互联网 发布:淘宝开通信用卡 编辑:程序博客网 时间:2024/04/30 00:15
题目描述
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.
样例输入
14 4192.168.0.0/16 1234192.168.1.0/24 1235192.168.1.0/23 1233192.168.0.0/23 1236192.168.2.0192.168.0.0192.168.1.0255.255.255.255
样例输出
12341233123565535
第一次在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
- 路由表linux routing table
- 东大OJ-麦森数
- 第六届山东省赛I题 Routing Table 字典树 NEU 1588 & UPC 3116 & SDUT 3259
- Multipath routing with Group table at mininet
- (OK) Problem With Android Configuration - routing table
- linux ip-route - routing table management
- 东大OJ-1430-PrimeNumbers
- 东大OJ-Max Area
- 东大OJ-快速排序
- 东大OJ-Prim算法
- Routing
- Routing
- Routing
- What Is The Meaning of 0.0.0.0 In Routing Table?
- DCE HOWTO linux netlink working with routing table
- 东大OJ-1391-Big big Power
- 东大OJ 2SAT 异或
- CQU汇编实验五(仓库管理系统)_基础题
- javascript splice()插入、删除或替换数组
- Android学习笔记(2):带预览的图片浏览器(GridView、ImageSwitch以及SimpleAdapter)
- linux ssh port forward
- UGUI - 关于ScrollContent 和 Grid使用
- 东大OJ-1588: Routing Table
- git 2.4.3 dependencies install
- iOS不同版本适配问题(#ifdef __IPHONE_7_0)
- A. Face Detection
- 用百度SDK获取地理位置和天气信息
- iOS 数据的本地存储-自定义对象的归档
- 设置textview中hint的大小
- 斗鱼TV协议分享
- 关于Solr的基本概念