题目描述

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.很像链式前向星的一种结构
#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;
}

转载于:https://www.cnblogs.com/weiyinfu/p/5013881.html

东大OJ-1588: Routing Table相关推荐

  1. (chap4 IP协议) 路由控制表(Routing Table)

    1. 定义 发送数据包基于网络层,需要目标端的IP地址,还需要类似于"指明路由器或主机"的信息,以便真正发往目标地址.保存这种信息的就是.实现IP通信的主机和路由器都必须持有一张这 ...

  2. 东大OJ 2SAT 异或

    看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{int ...

  3. hrbust 哈理工oj 1588 神医【贪心】

    神医 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 140(33 users) Total Accepted: 43(31 users ...

  4. [翻译:更新]Understanding Linux Network Internals - Table of Contents

    会在每一节翻译完成后,更新本目录的链接,没有链接的就是还没有翻译的. Linux内核源代码下载(linux-2.6.10): http://www.softpedia.com/progDownload ...

  5. Neutron 分布式虚拟路由(Neutron Distributed Virtual Routing)

    本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...

  6. 东大OJ-Max Area

    1034: Max Area 时间限制: 1 Sec  内存限制: 128 MB 提交: 40  解决: 6 [提交][状态][讨论版] 题目描述 又是这道题,请不要惊讶,也许你已经见过了,那就请你再 ...

  7. ZigBee Routing Tables 路由表

    以下内容摘自ZigBee 2015 Pro文档<docs-05-3474-21-0csg-zigbee-specification> 3.6.3.2 Routing Tables A Zi ...

  8. HCIE - Routing Switching v3.0 Outline

    Layer 2 Technologies STP 网桥ID(BID)最小者当选.网桥优先级取值越小,则网桥ID就越小.若优先级相同,则比较MAC地址. 在每一个非根交换机上选出一个根端口RP(Root ...

  9. RFC8402 Segment Routing Architecture 翻译

     黑色为原文 紫色为翻译 红色为校注 Internet Engineering Task Force (IETF)                                            ...

最新文章

  1. 在家搭建大数据分布式计算环境!
  2. iOS架构-C/C++lame库在Mac下编译通用静态库.a库(13)
  3. 对实施运维的一点心得体会
  4. mac中python怎么打开中文txt文件-mac在matplotlib中显示中文的操作方法
  5. ThinkPHP 框架培训资料
  6. 设置linux防火墙服务不开机启动,Linux关闭防火墙并设置开机启动/不启动
  7. CodeForces 906D (欧拉降幂)
  8. 【noip 2016】 蚯蚓(earthworm)
  9. java 生成csr_Java以编程方式生成CSR
  10. 微信支付JAVA DEMO 微信支付爬过的坑
  11. 拓端tecdat|WINBUGS对随机波动率模型进行贝叶斯估计与比较
  12. solaris 8 重置系统配置
  13. poi导出如何设定宽度_POI导出excel列宽自适应
  14. png变成矢量图_ps中如何在将图转换成矢量图
  15. android slidemenu 折叠效果,左侧菜单栏折叠展开效果-超级简单
  16. 蓝牙网络共享linux,在Linux系统上安装Blueman以通过蓝牙共享文件
  17. 计算机主机内部的结构,台式电脑主机内部结构介绍
  18. matlab中累乘,numpy中的裁剪、压缩和累乘
  19. 结构化随机森林 代码说明
  20. 阿里云物联网和移动OneNet初步使用感受

热门文章

  1. phpnow 更改mysql data文件夹路径
  2. @WebFilter()配置servlet访问出现404的原因
  3. P2479 [SDOI2010]捉迷藏
  4. C-Lodop的https扩展版,火狐下添加例外
  5. 基金理财系列6 之指数基金
  6. [原创] 在XP/2K 下实现 Win+Ctrl+Del 等键的屏蔽的方法,附源码与演示程序下载。...
  7. [转]2020年2月份Github上最热门的开源项目,速来围观
  8. c语言控制倒车雷达系统,倒车雷达控制系统设计
  9. 爬取上千个年度基金后,才知道这有多坑,千万别被人当韭菜给割了
  10. 关于自然排序Comparable 和 比较器排序Comparator