작성일 :

문제 링크

29701번 - 모스 부호

설명

모스 부호로 변환된 문자열이 주어질 때, 표를 이용해 원래 문자열을 출력하는 문제입니다.


접근법

각 모스 부호는 공백으로 구분되므로, 토큰을 순서대로 읽습니다.
표를 문자열-문자 매핑으로 저장해 각 토큰을 치환한 뒤 이어 붙입니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Text;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var tokens = Console.ReadLine()!.Split(' ', StringSplitOptions.RemoveEmptyEntries);

    var codes = new[] {
      ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
      "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
      "..-", "...-", ".--", "-..-", "-.--", "--..",
      ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----",
      "--..--", ".-.-.-", "..--..", "---...", "-....-", ".--.-."
    };
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.?:-@";

    var map = new Dictionary<string, char>();
    for (var i = 0; i < codes.Length; i++)
      map[codes[i]] = chars[i];

    var sb = new StringBuilder();
    for (var i = 0; i < n; i++)
      sb.Append(map[tokens[i]]);

    Console.WriteLine(sb.ToString());
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vector<string> codes = {
    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
    "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
    "..-", "...-", ".--", "-..-", "-.--", "--..",
    ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----",
    "--..--", ".-.-.-", "..--..", "---...", "-....-", ".--.-."
  };
  string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.?:-@";

  map<string, char> mp;
  for (int i = 0; i < (int)codes.size(); i++)
    mp[codes[i]] = chars[i];

  string res;
  res.reserve(n);
  for (int i = 0; i < n; i++) {
    string token; cin >> token;
    res.push_back(mp[token]);
  }

  cout << res << "\n";

  return 0;
}