置頂

我的 VPython 教學文件 (HackMD 版本)

VPython 教學文件目錄 安裝及測試 基本語法 等速度直線運動 自由落下 終端速度 水平抛射 使用For迴圈計算水平抛射資料 斜向抛射 圓周運動 簡諧運動 單擺 木塊彈簧系統分離 重力及簡諧 行星運動 相疊木塊 雙重簡諧運動 一維彈性碰撞 ...

熱門文章

2026年11月21日 星期六

ZeroJudge 解題筆記:e910.108 p10. 解碼問題

作者:王一哲
日期:2026年11月21日


ZeroJudge 題目連結:e910.108 p10. 解碼問題

解題想法


這題最直接的作法是用字典建立編號、字元對照表 $table$。接下來依序由測資給的字串 $s$ 讀取字元 $c$,存到一個暫存編號用的字串 $t$,如果 $t$ 為 $table$ 之中的某個 key 值,將 $table[t]$ 加入答案 $ans$ 之中,再清空 $t$。

Python 程式碼


使用時間約為 31 ms,記憶體約為 10.6 MB,通過測試。
def solve():
    import sys

    table = {"00": " ", 
             "010010": ".", 
             "01011101000": "!", 
             "01011110": ",",
             "0101111100100": "?", 
             "010111011": "0", 
             "110111100": "1",  
             "010111000": "2",
             "11011110111": "3", 
             "010111110011": "4", 
             "0101110101": "5", 
             "11011110100": "6",
             "01011101001": "7", 
             "11011110101": "8", 
             "0101111100101": "9", 
             "1000": "A",
             "110110": "B", 
             "10110": "C", 
             "11010": "D", 
             "1111": "E",
             "01010": "F", 
             "010110": "G", 
             "10101": "H", 
             "0110": "I",
             "01011111000": "J", 
             "010111111": "K", 
             "10100": "L", 
             "01000": "M",
             "0111": "N", 
             "1001": "O", 
             "010011": "P", 
             "010111001": "Q",
             "11100": "R", 
             "11101": "S", 
             "1100": "T", 
             "101111": "U",
             "11011111": "V", 
             "1101110": "W", 
             "0101111101": "X", 
             "101110": "Y",
             "11011110110": "Z"}

    result = []
    data = sys.stdin.read().split()
    ptr = 0
    while ptr < len(data):
        s = data[ptr]
        ptr += 1
        res = []
        t = ""
        for c in s:
            t += c
            if t in table:
                res.append(table[t])
                t = ""
        
        result.append("".join(res) + "\n")
    
    sys.stdout.write("".join(result))

if __name__ == "__main__":
    solve()


使用時間約為 49 ms,記憶體約為 10.6 MB,通過測試。
table = {"00": " ", 
         "010010": ".", 
         "01011101000": "!", 
         "01011110": ",",
         "0101111100100": "?", 
         "010111011": "0", 
         "110111100": "1",  
         "010111000": "2",
         "11011110111": "3", 
         "010111110011": "4", 
         "0101110101": "5", 
         "11011110100": "6",
         "01011101001": "7", 
         "11011110101": "8", 
         "0101111100101": "9", 
         "1000": "A",
         "110110": "B", 
         "10110": "C", 
         "11010": "D", 
         "1111": "E",
         "01010": "F", 
         "010110": "G", 
         "10101": "H", 
         "0110": "I",
         "01011111000": "J", 
         "010111111": "K", 
         "10100": "L", 
         "01000": "M",
         "0111": "N", 
         "1001": "O", 
         "010011": "P", 
         "010111001": "Q",
         "11100": "R", 
         "11101": "S", 
         "1100": "T", 
         "101111": "U",
         "11011111": "V", 
         "1101110": "W", 
         "0101111101": "X", 
         "101110": "Y",
         "11011110110": "Z"}

while True:
    try:
        s = input()
    except EOFError:
        break

    res = []
    t = ""
    for c in s:
        t += c
        if t in table:
            res.append(table[t])
            t = ""
    
    print("".join(res))


C++ 程式碼


使用時間約為 9 ms,記憶體約為 3.8 MB,通過測試。
#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    unordered_map<string, string> table = 
        {{"00", " "}, 
         {"010010", "."}, 
         {"01011101000", "!"}, 
         {"01011110", ","},
         {"0101111100100", "?"}, 
         {"010111011", "0"}, 
         {"110111100", "1"}, 
         {"010111000", "2"},
         {"11011110111", "3"}, 
         {"010111110011", "4"}, 
         {"0101110101", "5"}, 
         {"11011110100", "6"},
         {"01011101001", "7"}, 
         {"11011110101", "8"}, 
         {"0101111100101", "9"}, 
         {"1000", "A"},
         {"110110", "B"}, 
         {"10110", "C"}, 
         {"11010", "D"}, 
         {"1111", "E"},
         {"01010", "F"}, 
         {"010110", "G"}, 
         {"10101", "H"}, 
         {"0110", "I"},
         {"01011111000", "J"}, 
         {"010111111", "K"}, 
         {"10100", "L"}, 
         {"01000", "M"},
         {"0111", "N"}, 
         {"1001", "O"}, 
         {"010011", "P"}, 
         {"010111001", "Q"},
         {"11100", "R"}, 
         {"11101", "S"}, 
         {"1100", "T"}, 
         {"101111", "U"},
         {"11011111", "V"}, 
         {"1101110", "W"}, 
         {"0101111101", "X"}, 
         {"101110", "Y"},
         {"11011110110", "Z"}};

    string s, t, ans;  // 輸入字串 s,暫存待解碼的字串 t,答案 ans
    while(getline(cin, s)) {
        t.clear();
        ans.clear();
        for(char c : s) {
            t += c;
            if (table.count(t) == 1) {
                ans += table[t];
                t.clear();
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


沒有留言:

張貼留言