日期: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;
}
沒有留言:
張貼留言