提交记录 10888


用户 题目 状态 得分 用时 内存 语言 代码长度
skylines router32. 测测你的路由器 Compile Error 0 0 ns 0 KB C++11 1.27 KB
提交时间 评测时间
2019-10-09 16:46:16 2020-08-01 02:35:12
#include "router.h"
struct Trie{
    struct node{
        node* son[2];
        unsigned via;
        node(){
            via=0;
            son[0]=nullptr;
            son[1]=nullptr;
        }
    };
    //const int id[32]={7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8,23,22,21,20,19,18,17,16,31,30,29,28,27,26,25,24};
    node *root;
    void insert(unsigned addr,int len,unsigned nxt){
        if(root==nullptr)
        {
            root = new node();
        }
        node *tmp=root;
        bool x;
        for(int i=0;i<len;i++)
        {
            x=addr>>(31-i)&1;
            if(tmp->son[x]==nullptr)
                tmp->son[x] = new node();
            tmp=tmp->son[x];
        }
        tmp->via=nxt;
    }
    unsigned query(unsigned addr){
        node *tmp=root;
        unsigned ans = 0;
        bool x;
        for(int i=31;i>=0;i--)
        {
            x=addr>>i&1;
            if(tmp->son[x]==nullptr) 
                return ans;
            tmp=tmp->son[x];
            if(tmp->via!=0)
                ans=tmp->via;
        }
        return ans;
    }
}trie;
void init(int n, int q, const RoutingTableEntry *a){
    for(int i=0;i<n;i++){
        trie.insert(htonl(a[i].addr),a[i].len,a[i].nexthop);
    }
}
unsigned query(unsigned addr){
    return trie.query(htonl(addr););
}

CompilationN/AN/ACompile ErrorScore: N/A


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