モールス符号を使う方法
教育や通信練習のために、テキストのモールス符号へのエンコードと復号を行います。
问题
テキストをモールス符号に変換する、またはモールス符号をテキストに戻す必要があります。
前提条件
- モールス符号に関する知識
- 点と線の理解
ステップ
-
1. Learn the Morse code mapping
Each letter and digit maps to a sequence of dots (.) and dashes (-). A is .-, B is -..., etc. Letters are separated by a space, words by a larger gap (often represented as / or three spaces). Numbers and some punctuation also have mappings.
入力HELLO输出.... . .-.. .-.. --- -
2. Encode text to Morse
Convert each character to its Morse sequence using the mapping. Separate letters with a single space and words with a slash or multiple spaces. Uppercase the input first, as Morse is case-insensitive. Unknown characters can be skipped or marked with a placeholder.
入力SOS HELP输出... --- ... / .... . .-.. .--. -
3. Decode Morse to text
Split the Morse string on word separators first, then on letter separators. Map each Morse sequence back to its character. Handle ambiguous separators: a single space separates letters, a slash or multiple spaces separates words.
入力... --- ...输出SOS
期待される結果
入力テキストのモールス信号への正しいエンコード、またはその逆のデコード。
よくあるエラー
- Word boundaries lost in decoding
- Use a clear word separator (like /) between words. A single space between letters and / between words is the common convention.
- Lowercase letters not handled
- Morse code is case-insensitive. Uppercase the input before encoding, and output uppercase when decoding.
トラブルシューティング指引
- If decoding produces wrong characters, verify the Morse mapping table.
- Ensure consistent use of separators: one space between letters, / or three spaces between words.
セキュリティ上の制限
- Morse code is a simple substitution encoding with no security properties.