题目描述

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;
}

东大OJ-1588: Routing Table的更多相关文章

  1. 东大OJ 2SAT 异或

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

  2. 理解 OpenStack 高可用(HA)(3):Neutron 分布式虚拟路由(Neutron Distributed Virtual Routing)

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

  3. 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 ...

  4. 东大OJ-Max Area

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

  5. 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 ...

  6. ASP.NET Web API中的Routing(路由)

    [译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...

  7. Study之6 Neutron(配置使用 Routing)-devstack

    ●Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 firewall 和 floating ip 服务. l3 agent 需要正确 ...

  8. Routing Manager for WCF4 z

    http://www.codeproject.com/Articles/77198/Routing-Manager-for-WCF Download source Contents Features ...

  9. Classless Interdomain Routing (CIDR)

    IP Address Problems IP Address Exhaustion Class A, B, and C address structure inefficient Class B to ...

随机推荐

  1. Win8 忘记密码 解决办法【在E450c实测有效】

      工具/原料 已经刻录通用PE工具箱的U盘 方法/步骤 1 首先来看一下,问题产生的状况.出现登陆界面,由于密码忘记,怎么输入密码,老是提示密码错误 2 接下来,按住Shift,电脑关机重启,也就是 ...

  2. HashMap和HashSet

    Java使用Set接口来描述集合,而Set中每一个数据元素都是唯一的. HashSet散列集合 Hash算法:把任意长度输入,通过散列算法,变换成固定长度的输出即散列值.对不同类型信息,散列值公式也是 ...

  3. Spring MVC @ResponseBody返回中文字符串乱码问题

    朋友做小项目练手的时候遇到的,着实让他郁闷够呛..这个问题的确很恶心.. 项目中引用的json包,直接用@ResponseBody注解返回json字符串..有关这个的乱码问题网上很多,各种花样各种转码 ...

  4. [转]simple sample to create and use widget for nopcommerce

    本文转自:http://badpaybad.info/simple-sample-to-create-and-use-widget-for-nopcommerce Here is very simpl ...

  5. Security Tools (Contain CTF tools)

    From now on I will start to have fun with CTF and other security games or challenges. And I am going ...

  6. POJ 1696 Space Ant 【极角排序】

    题意:平面上有n个点,一只蚂蚁从最左下角的点出发,只能往逆时针方向走,走过的路线不能交叉,问最多能经过多少个点. 思路:每次都尽量往最外边走,每选取一个点后对剩余的点进行极角排序.(n个点必定能走完, ...

  7. [No00002B]知乎精选:如果兔子都在拼命奔跑,乌龟该如何前进

    最近看到友人分享的一篇好文章,看了不下三遍,想开了很多的事情…… 在这个世界上永远存在一些比你更加牛的人,无论什么方面.如果把人生比作攀登,也许你穷其一生可以达到一定的高度,但对某些人来说珠峰都不成问 ...

  8. [No00000C]Word快捷键大全 Word2013/2010/2007/2003常用快捷键大全

    Word对于我们办公来说,是不可缺少的办公软件,因为没有它我们可能无法进行许多任务.所以现在的文员和办公室工作的人,最基础的就是会熟悉的使用Office办公软件.在此,为提高大家Word使用水平,特为 ...

  9. 改变TableView中的分割线位置

    加上以下代码,可以让系统的分割线位置置于起始位置 #pragma mark --- 设置分割线位置为起始位置-(void)viewDidLayoutSubviews{    if ([self.tab ...

  10. PL/SQL Block Structure

    [顶]ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素(为山九仞,岂一日之功) 继上四篇:ORACLE PL/SQL编程之八:把触发器说透                ORAC ...