提交记录 13350


用户 题目 状态 得分 用时 内存 语言 代码长度
lcyxds router32. 测测你的路由器 Wrong Answer 50 141.264 ms 17280 KB C++ 1.47 KB
提交时间 评测时间
2020-08-01 11:28:44 2020-08-01 11:28:46
#include "router.h"
#include <iostream>

/*
typedef struct {
    unsigned addr;
    unsigned char len;
    char pad[3];  // Padding for memory alignment
    unsigned nexthop;
} __attribute__((packed)) RoutingTableEntry;
*/

using namespace std;

struct Node {
	bool end;
	int hop;
	Node* next[2];
};

Node* _root = new Node;

void Insert(RoutingTableEntry a) {
//	cout << "Insert " << a.addr << endl;
	Node* c = _root;
	int solo;
	for (int i = 1; i <= a.len; i++) {
		solo = a.addr>>(32-i)&1;
//		cout << i << ',' << solo << endl;
		if (!c->next[solo]) {
			c->next[solo] = new Node;
			c->next[solo]->end = false;
			c->next[solo]->next[0] = 0;
			c->next[solo]->next[1] = 0;
		}
		c = c->next[solo];
	}
	c->end = true;
	c->hop = a.nexthop;
//	cout << "Insertion end" << endl;
}

//RoutingTableEntry Input() {
//	RoutingTableEntry a;
//	unsigned x, y, z;
//	cin >> hex >> x >> dec >> y >> z;
//	a.addr = x;
//	a.len = y;
//	a.nexthop = z;
//	return a;
//}

void init(int n, int q, const RoutingTableEntry *a) {
	_root->end = false;
	_root->next[0] = 0;
	_root->next[1] = 0;
	for (int i = 0; i < n; i++) {
		Insert(a[i]);
	}
//	cout << "init end" << endl;
}

unsigned query(unsigned addr) {
//	cout << "Query " << addr << endl;
	unsigned res = 0;
	Node* c = _root;
	int solo;
	if (c->end) {
		res = c->hop;
	}
	for (int i = 1; i <= 32; i++) {
		solo = addr>>(32-i)&1;
//		cout << i << ',' << solo << endl;
		c = c->next[solo];
		if (!c) return res;
		if (c->end) {
			res = c->hop;
		}
	}
	return res;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #133.71 us36 KBAcceptedScore: 25

Testcase #243.948 ms16 MB + 896 KBAcceptedScore: 25

Testcase #392.811 ms16 MB + 896 KBWrong AnswerScore: 0

Testcase #4141.264 ms16 MB + 896 KBWrong AnswerScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-03-24 12:14:32 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠