东大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 ...
随机推荐
- android xml 布局错误(黑掉了)Missing styles. Is the correct theme chosen for this layout?
发现在调整页面的时候 ,老是报以下错误,导致无法静态显示ui效果. Missing styles. Is the correct theme chosen for this layout? Use t ...
- ELF Format 笔记(六)—— 字符串表
ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 字符串表中包含若干以 null 结尾的字符串,这些字符串通常是 symbol 或 section 的名字.当 ...
- 关于TCP连接建立与终止那点事
0. 前言 最近在处理公司遗留项目的时候发现自己对TCP协议一点都不懂,所以补了点关于TCP连接的建立和终止的内容,这里简单写下自己了解的部分,省略了报文序号确认序号这些无关的字段,主要讨论TCP状态 ...
- 关于C语言中的位域
有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态,用一位二进位即可.为了节省存储空间,并使处理简便,C语言提供了一种数据结构,称 ...
- 如何在报表权限中使用session
1. 问题描述 权限中使用session,一般是用来存放用户名和密码,下面以报表开发工具FineReport为例,分两种情况介绍用户名和密码的保存: 2. 同一应用下session 由于session ...
- 实现跨云应用——基于DNS的负载均衡
“公有云可以作为传统IT资源的延展,能帮助客户应对不断变化的需求”——这是我们在向客户介绍公有云产品时经常说的一句话.我们来看一个具体的需求: 某客户有一个web站点,部署在自有的数据中心(on-pr ...
- 详细说说 Google Test Certified 的各级——Level 2,3
转载请联系作者,谢谢! No releases with red tests基于Level1搭建的持续集成,持续发布选用的CL(changelist)就可以取自CI系统最后跑通的CL,因为持续集成 ...
- 第一天接触Orchard
第一天接触Orchard 为什么要研究Orchard 呢? 楼主是因为要研究下最新的ASP.NET技术, Orchard 用的人多,历史也比较久,算是老牌了 再就是中文资料比较多,Orchard 有中 ...
- luogu[1279]字串距离
题目描述 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为”abcbcd”,则字符串“abcb□cd”,“□a□bcbcd□”和“abcb□cd□”都是X ...
- python中的字符串操作
#!/usr/bin/python # -*- coding: UTF-8 -*- ''' str.capitalize() ''' str = 'this is a string example' ...