提交记录 10744


用户 题目 状态 得分 用时 内存 语言 代码长度
Zhengyi_Wang router32. 测测你的路由器 Accepted 100 315.556 ms 67756 KB C++ 1.26 KB
提交时间 评测时间
2019-09-28 19:35:10 2020-08-01 02:25:17
#include "router.h"
#include <netinet/in.h> 
#include <cstdlib>
unsigned ones_prefix[33];
unsigned mask_addr[33];
int maxlen=-1;
int n;
int q;
RoutingTableEntry *a;

struct trie_node
{
	unsigned nexthop;
	unsigned isvalid;
	trie_node* son[2];
};
trie_node* root=new trie_node;
void insert_node(unsigned addr,unsigned len,unsigned nexthop){
	trie_node* tmp_node=root;
	for (unsigned i=0;i<len;i++){
		int dir=(addr>>(31-i))&1;
		if (tmp_node->son[dir]==NULL){
			tmp_node->son[dir]=new trie_node;
			tmp_node=tmp_node->son[dir];
			tmp_node->son[0]=tmp_node->son[1]=NULL;
			tmp_node->nexthop=tmp_node->isvalid=0;
		}
		else{
			tmp_node=tmp_node->son[dir];
		}
	}
	tmp_node->nexthop=nexthop;
	tmp_node->isvalid=1;
}



void init(int _n, int _q, const RoutingTableEntry *_a) {
	n=_n;
	q=_q;
	a=(RoutingTableEntry*)_a;

	root->son[0]=root->son[1]=NULL;
	root->nexthop=root->isvalid=0;
	for (int i=0;i<n;i++){
		insert_node(htonl(a[i].addr),a[i].len,a[i].nexthop);
	}
}


unsigned query(unsigned addr) {
	addr=htonl(addr);
	trie_node* tmp_node=root;
	unsigned ans=root->nexthop;
	for (int i=0;i<32;i++){
		int dir=(addr>>(31-i))&1;
		if (tmp_node->son[dir]==NULL){
			break;
		}
		tmp_node=tmp_node->son[dir];
		if (tmp_node->isvalid==1){
			ans=tmp_node->nexthop;
		}
	}


	return ans;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #112.62 us24 KBAcceptedScore: 25

Testcase #248.001 ms66 MB + 172 KBAcceptedScore: 25

Testcase #3182.136 ms66 MB + 172 KBAcceptedScore: 25

Testcase #4315.556 ms66 MB + 172 KBAcceptedScore: 25


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:35:48 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠