[백준 26502] Decoder (C#, C++) - soo:bak
작성일 :
문제 링크
설명
모음을 다른 모음으로 치환한 암호문이 주어질 때, 주어진 테이블을 역으로 적용해 원문을 출력하는 문제입니다.
접근법
복호화는 암호화의 역과정입니다. 문자열의 각 문자를 확인해서 모음이면 원래 모음으로 되돌리고, 그 외에는 그대로 둡니다. 대문자 모음도 같은 방식으로 처리합니다.
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
using System;
using System.Text;
class Program {
static char Decode(char c) {
return c switch {
'y' => 'a', 'a' => 'e', 'e' => 'i', 'i' => 'o', 'o' => 'u', 'u' => 'y',
'Y' => 'A', 'A' => 'E', 'E' => 'I', 'I' => 'O', 'O' => 'U', 'U' => 'Y',
_ => c
};
}
static void Main() {
var n = int.Parse(Console.ReadLine()!);
for (var i = 0; i < n; i++) {
var line = Console.ReadLine()!;
var sb = new StringBuilder(line.Length);
foreach (var ch in line) {
sb.Append(Decode(ch));
}
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
#include <bits/stdc++.h>
using namespace std;
char decode(char c) {
switch (c) {
case 'y': return 'a'; case 'a': return 'e'; case 'e': return 'i';
case 'i': return 'o'; case 'o': return 'u'; case 'u': return 'y';
case 'Y': return 'A'; case 'A': return 'E'; case 'E': return 'I';
case 'I': return 'O'; case 'O': return 'U'; case 'U': return 'Y';
default: return c;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
cin.ignore();
string line;
for (int i = 0; i < n; i++) {
getline(cin, line);
for (char& c : line)
c = decode(c);
cout << line << "\n";
}
return 0;
}