1 solutions

  • 0
    @ 2025-11-23 14:59:21

    提供的代码可以直接用,辅助函数没有必要用。

    对于动态表的处理可以用队列,访问元素如果在动态表可以先把队列复制一次,然后一直pop临时队列里的元素直到size()==id-S的时候,队首元素就是需要的,这样做是允许的因为数据量够小,可以随便暴力。

    对于解码的问题,可以先写一个将二进制串解码的函数,具体做法就是利用提供代码,从根开始如果是0就往左儿子走,1就往右儿子走,如果走到了树的叶子也就是now->data!='\0' 的时候输出到解码串,此时再将节点重置到根节点,重复以上操作即可。

    对于哈夫曼编码串用了十六进制,可以先把每个字符转成数字,再二进制分解,读串最后两位来获取最后加的零的个数,使用string自带的erase,substr等函数操作。 代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    map<char, string> code;
    struct Node {
        char data;
        shared_ptr<Node> left;
        shared_ptr<Node> right;
    
        Node(char d) : data(d), left(nullptr), right(nullptr) {}
        Node() : data('\0'), left(nullptr), right(nullptr) {}
    };
    shared_ptr<Node> rebuildHuffmanTree(const string& s, int& index) {
        if (index >= s.length()) return nullptr;
    
        if (s[index] == '1') {
            index++; // 跳过'1'
            char ch = s[index++]; // 读取字符
            return make_shared<Node>(ch);
        } else if (s[index] == '0') {
            index++; // 跳过'0'
            auto node = make_shared<Node>();
            node->left = rebuildHuffmanTree(s, index);
            node->right = rebuildHuffmanTree(s, index);
            return node;
        }
        return nullptr;
    }
    vector<pair<string, string>> still_table;
    queue<pair<string, string>> dynamic_table;
    int main() {
        int S, D;
        cin >> S >> D;
        still_table.emplace_back("", "");
        for (int i = 1; i <= S; i++) {
            string key, value;
            cin >> key >> value;
            still_table.emplace_back(key, value);
        }
        string encodedTree;
        cin >> encodedTree; // 示例:0表示内部节点,1表示叶子节点
        int index = 0;
        auto root = rebuildHuffmanTree(encodedTree, index);
        auto encode = [&](string secret) -> string {
            // 从哈夫曼树的根节点一路往下找,找到叶子节点就在encodestring里面加内容
            string encodestr = "";
            auto now = root;
            for (auto ch : secret) {
                if (ch == '0') {
                    now = now->left;
                } else {
                    now = now->right;
                }
                if (now->data != '\0') {
                    encodestr += now->data;
                    now = root;
                }
            }
            return encodestr;
        };
        auto solve = [&](string init_secret) -> string {
            if (init_secret[0] != 'H') return init_secret;
            if (init_secret[1] == 'H') return init_secret.substr(1);
            init_secret.erase(0, 1);
            int Size = init_secret.size();
            int more_zero = init_secret[Size - 1] - '0';
            string binary;
            for (int i = 0; i < Size - 2; i++) {
                char ch = init_secret[i];
                if (ch > '9') ch = ch - 'a' + 10;
                else ch = ch - '0';
                for (int j = 0; j < 4; j++) binary.push_back(((ch >> (3 - j)) & 1) + '0');
            }
            // cerr<<binary<<"->";
            //  删除最后more_zero个字符
            binary.erase(binary.size() - more_zero, more_zero);
            // cerr<<binary<<":"<<encode(binary)<<endl;
            return encode(binary);
        };
        auto get = [&](int id) {
            if (id <= S) {
                return still_table[id];
            } else {
                auto que = dynamic_table;
                id -= S;
                while (que.size() > id) que.pop();
                return que.front();
            }
        };
        int N;
        cin >> N;
        while (N--) {
            int op;
            cin >> op;
            if (op == 1) {
                int id;
                cin >> id;
                auto [key, value] = get(id);
                cout << key << ": " << value << endl;
            } else {
                string key, value;
                int x;
                cin >> x;
                if (x) {
                    key = get(x).first;
                    string seg_name;
                    cin >> seg_name;
                    value = solve(seg_name);
                } else {
                    cin >> key >> value;
                    key = solve(key);
                    value = solve(value);
                }
                cout << key << ": " << value << endl;
                if (op == 2) {
                    dynamic_table.push({key, value});
                    if (dynamic_table.size() > D) { dynamic_table.pop(); }
                }
            }
        }
        return 0;
    }
    
    

    Information

    ID
    257
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    4
    Tags
    # Submissions
    182
    Accepted
    20
    Uploaded By