提交记录 14215


用户 题目 状态 得分 用时 内存 语言 代码长度
jianglin router32. 测测你的路由器 Wrong Answer 25 12.455 ms 9716 KB C++ 1.66 KB
提交时间 评测时间
2020-09-18 16:35:23 2020-09-18 16:35:29

#include "router.h"
struct RoutingTableNode {
    RoutingTableNode *nx[2];
    bool hasRoute;
    unsigned int nexthop;
    RoutingTableNode() {
        nx[0] = nx[1] = nullptr;
        hasRoute = false;
    }
    ~RoutingTableNode() {
        if (nx[0]) {
            delete nx[0];
        }
        if (nx[1]) {
            delete nx[1];
        }
    }
} *root = nullptr;
void init(int n, int q, const RoutingTableEntry *a) {
    root = new RoutingTableNode;
    for (int i = 0; i < n; i++) {
        RoutingTableNode *now = root;
        unsigned int addr = a[i].addr;
        addr = (addr >> 24) + (((addr >> 16) & 0xff) << 8) +
         (((addr >> 8) & 0xff) << 16) + ((addr & 0xff) << 24);
        for (int i = 0; i < a[i].len; i++) {
            bool nw = addr & 0x80000000;
            addr <<= 1;
            if (!now->nx[nw]) {
                now->nx[nw] = new RoutingTableNode;
            }
            now = now->nx[nw];
        }
        now->hasRoute = true;
        now->nexthop = a[i].nexthop;
    }
}

unsigned query(unsigned addr) {
    RoutingTableNode *now = root;
    addr = (addr >> 24) + (((addr >> 16) & 0xff) << 8) +
         (((addr >> 8) & 0xff) << 16) + ((addr & 0xff) << 24);
    RoutingTableNode *found = nullptr;
    if (now->hasRoute) {
        found = now;
    }
    for (int i = 0; i < 32; i++) {
        bool nw = addr & 0x80000000;
        addr <<= 1;
        if (!now->nx[nw]) {
            if (!found) {
                return 0;
            }
            return found->nexthop;
        }
        now = now->nx[nw];
        if (now->hasRoute) {
            found = now;
        }
    }
    if (!found) {
        return 0;
    }
    return found->nexthop;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #112.11 us24 KBAcceptedScore: 25

Testcase #23.562 ms9 MB + 500 KBWrong AnswerScore: 0

Testcase #38.007 ms9 MB + 500 KBWrong AnswerScore: 0

Testcase #412.455 ms9 MB + 500 KBWrong AnswerScore: 0


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