작성일 :

문제 링크

4583번 - 거울상

설명

주어진 단어가 거울상으로 표현 가능한지 확인하고, 가능하면 거울상 결과를 출력하는 문제입니다.


접근법

각 문자를 거울상 문자로 치환할 수 있어야 합니다.

치환한 뒤 문자열을 뒤집어 출력하고, 불가능한 문자가 있으면 INVALID를 출력합니다.


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
30
31
32
33
34
using System;
using System.Collections.Generic;

class Program {
  static void Main() {
    var map = new Dictionary<char, char> {
      {'b','d'}, {'d','b'}, {'p','q'}, {'q','p'},
      {'i','i'}, {'o','o'}, {'v','v'}, {'w','w'}, {'x','x'}
    };

    while (true) {
      var s = Console.ReadLine()!;
      if (s == "#") break;

      var arr = new char[s.Length];
      var ok = true;
      for (var i = 0; i < s.Length; i++) {
        if (!map.TryGetValue(s[i], out var ch)) {
          ok = false;
          break;
        }
        arr[i] = ch;
      }

      if (!ok) {
        Console.WriteLine("INVALID");
        continue;
      }

      Array.Reverse(arr);
      Console.WriteLine(new string(arr));
    }
  }
}

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
33
34
35
#include <bits/stdc++.h>
using namespace std;

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

  map<char, char> mp = {
    {'b','d'}, {'d','b'}, {'p','q'}, {'q','p'},
    {'i','i'}, {'o','o'}, {'v','v'}, {'w','w'}, {'x','x'}
  };

  string s;
  while (cin >> s) {
    if (s == "#") break;

    string t;
    bool ok = true;
    for (char c : s) {
      auto it = mp.find(c);
      if (it == mp.end()) { ok = false; break; }
      t.push_back(it->second);
    }

    if (!ok) {
      cout << "INVALID\n";
      continue;
    }

    reverse(t.begin(), t.end());
    cout << t << "\n";
  }

  return 0;
}