When a notice is stuck on the inside of a shop's glass door, people outside see it flipped left to right.
So you will prepare mirror lettering to stick on the inside, so that it reads correctly from outside.
The first line gives the number of lines n, and the next n lines give the notice, one line each. The notice is written left-aligned.
Make the mirror lettering of each notice line and print them in the same order as the input. The order of lines does not change.
Mirror lettering first reverses the order of the characters of the line, from the last character to the first.
Then the characters whose shape turns into each other in a mirror are swapped within their pairs.
The seven pairs are ( and ), [ and ], { and }, < and >, / and \, lowercase b and d, and lowercase p and q.
For example ( becomes ) and ) becomes (. Characters without a pair (spaces, digits, uppercase letters and others) stay as they are.
Left-aligned text flipped left to right becomes right-aligned. So if L is the length of the longest line, print each line with (L − the length of that line) spaces in front. The longest line gets no spaces.
Input
1 ≤ n ≤ 50. Each line has length 1 to 200, uses only ASCII characters (codes 32–126), and does not start or end with a space
Output
n lines
Example 1 · stdin
3
(open) 9-18
<- push door
bread & pie
stdout
81-9 (neqo)
roob hsuq ->
eiq & baerd
Example 2 · stdin
2
a/b\c
[sale] {today}
stdout
c/d\a
{yabot} [elas]
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Reversing: Python s[::-1], Java new StringBuilder(s).reverse(), C# Array.Reverse on a char array then new string(c), C++ std::reverse(s.begin(), s.end()), Craykon s.Reverse() (뒤집기). JS/TS strings have no reverse, so go through an array: [...s].reverse().join(""). The trap is swapping. Replacing in turn, s.replace("(", ")").replace(")", "("), turns the ) you just made back into (, so both end up as (. The input uses only codes 32–126, so park one side on a character that cannot appear in the input (code 1 here) and replace three times: a → parked, b → a, parked → b. A single pass that looks at each character and appends its partner (or the character itself) to a new string is just as correct. Replace-all: Python replace, JS replaceAll (in TS split(a).join(b) is also common), Java replace(char, char), C# Replace(char, char), C++ std::replace from <algorithm>, Craykon s.Replace(a, b) (바꾸기). C has no replace function, so it is natural to put the paired character in while swapping from both ends to reverse. A backslash must be written twice inside a string literal, "\\", to be one character (true in every language, Craykon too). For right alignment you only know the longest length after making every line, so collect the lines and print them at the end. Padding the front: Python s.rjust(L), JS/TS s.padStart(L), C# s.PadLeft(L), C printf("%*s", L, s), Craykon s.PadLeft(L) (앞채우기); in Java and C++ put (L − length) spaces in front (Java " ".repeat(k), C++ std::string(k, ' ')). Padding the end (padEnd) adds only trailing spaces, so the text stays left-aligned. A mirror flips only left and right, so do not reverse the order of the lines.
Pythonmain.py · 19 lines
TMP = "\x01" # 입력에 나오지 않는 글자(입력은 코드 32~126만 쓴다)를 잠깐 맡아 두는 자리로 쓴다PAIRS = [("(", ")"), ("[", "]"), ("{", "}"), ("<", ">"), ("/", "\\"), ("b", "d"), ("p", "q")]def swap(s, a, b): """s 안의 a와 b를 서로 맞바꾼다. a → b, b → a를 차례로 하면 둘 다 a가 되어 버린다.""" return s.replace(a, TMP).replace(b, a).replace(TMP, b)n = int(input())out = []for _ in range(n): s = input()[::-1] # 좌우를 뒤집는다(줄의 차례는 그대로) for a, b in PAIRS: s = swap(s, a, b) out.append(s)width = max(len(s) for s in out) # 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다print("\n".join(s.rjust(width) for s in out))
JavaScriptmain.js · 17 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const TMP = "\x01"; // 입력에 나오지 않는 글자(입력은 코드 32~126만 쓴다)를 잠깐 맡아 두는 자리로 쓴다const PAIRS = [["(", ")"], ["[", "]"], ["{", "}"], ["<", ">"], ["/", "\\"], ["b", "d"], ["p", "q"]];// s 안의 a와 b를 서로 맞바꾼다. a → b, b → a를 차례로 하면 둘 다 a가 되어 버린다const swap = (s, a, b) => s.replaceAll(a, TMP).replaceAll(b, a).replaceAll(TMP, b);const out = [];for (let k = 1; k <= n; k++) { let s = [...lines[k]].reverse().join(""); // 문자열에는 reverse가 없어 배열로 뒤집는다 for (const [a, b] of PAIRS) s = swap(s, a, b); out.push(s);}// 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다const width = Math.max(...out.map((s) => s.length));console.log(out.map((s) => s.padStart(width)).join("\n"));
TypeScriptmain.ts · 19 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const TMP = "\x01"; // 입력에 나오지 않는 글자(입력은 코드 32~126만 쓴다)를 잠깐 맡아 두는 자리로 쓴다const PAIRS: [string, string][] = [["(", ")"], ["[", "]"], ["{", "}"], ["<", ">"], ["/", "\\"], ["b", "d"], ["p", "q"]];// s 안의 a와 b를 서로 맞바꾼다. a → b, b → a를 차례로 하면 둘 다 a가 되어 버린다const swap = (s: string, a: string, b: string): string => s.split(a).join(TMP).split(b).join(a).split(TMP).join(b);const out: string[] = [];for (let k = 1; k <= n; k++) { let s: string = [...lines[k]].reverse().join(""); // 문자열에는 reverse가 없어 배열로 뒤집는다 for (const [a, b] of PAIRS) s = swap(s, a, b); out.push(s);}// 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다const width: number = Math.max(...out.map((s) => s.length));console.log(out.map((s) => s.padStart(width)).join("\n"));
JavaMain.java · 31 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { static final char TMP = '\u0001'; // 입력에 나오지 않는 글자(입력은 코드 32~126만 쓴다)를 잠깐 맡아 두는 자리로 쓴다 static final String PAIRS = "()[]{}<>/\\bdpq"; // 두 글자씩 짝 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); String[] mirrored = new String[n]; int width = 0; for (int k = 0; k < n; k++) { String s = new StringBuilder(br.readLine()).reverse().toString(); // 좌우를 뒤집는다 for (int i = 0; i < PAIRS.length(); i += 2) { char a = PAIRS.charAt(i), b = PAIRS.charAt(i + 1); // a → b, b → a를 차례로 하면 둘 다 a가 되므로 잠깐 TMP에 맡긴다 s = s.replace(a, TMP).replace(b, a).replace(TMP, b); } mirrored[k] = s; width = Math.max(width, s.length()); } // 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다 StringBuilder out = new StringBuilder(); for (String s : mirrored) { out.append(" ".repeat(width - s.length())).append(s).append('\n'); } System.out.print(out); }}
C#Main.cs · 33 lines
using System;using System.Text;class Program{ const char Tmp = '\u0001'; // 입력에 나오지 않는 글자(입력은 코드 32~126만 쓴다)를 잠깐 맡아 두는 자리로 쓴다 const string Pairs = "()[]{}<>/\\bdpq"; // 두 글자씩 짝 static void Main() { int n = int.Parse(Console.ReadLine().Trim()); var mirrored = new string[n]; int width = 0; for (int k = 0; k < n; k++) { char[] c = Console.ReadLine().ToCharArray(); Array.Reverse(c); // 좌우를 뒤집는다 string s = new string(c); for (int i = 0; i < Pairs.Length; i += 2) { char a = Pairs[i], b = Pairs[i + 1]; // a → b, b → a를 차례로 하면 둘 다 a가 되므로 잠깐 Tmp에 맡긴다 s = s.Replace(a, Tmp).Replace(b, a).Replace(Tmp, b); } mirrored[k] = s; width = Math.Max(width, s.Length); } // 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다 var sb = new StringBuilder(); foreach (string s in mirrored) sb.Append(s.PadLeft(width)).Append('\n'); Console.Write(sb); }}
C++main.cpp · 32 lines
#include <algorithm>#include <iostream>#include <string>#include <vector>int main() { const char tmp = '\x01'; // 입력에 나오지 않는 글자(입력은 코드 32~126만 쓴다)를 잠깐 맡아 두는 자리로 쓴다 const std::string pairs = "()[]{}<>/\\bdpq"; // 두 글자씩 짝 int n; std::cin >> n; std::string s; std::getline(std::cin, s); // n 뒤의 줄바꿈을 버린다 std::vector<std::string> mirrored; std::size_t width = 0; for (int k = 0; k < n; k++) { std::getline(std::cin, s); std::reverse(s.begin(), s.end()); // 좌우를 뒤집는다 for (std::size_t i = 0; i < pairs.size(); i += 2) { char a = pairs[i], b = pairs[i + 1]; // a → b, b → a를 차례로 하면 둘 다 a가 되므로 잠깐 tmp에 맡긴다 std::replace(s.begin(), s.end(), a, tmp); std::replace(s.begin(), s.end(), b, a); std::replace(s.begin(), s.end(), tmp, b); } width = std::max(width, s.size()); mirrored.push_back(s); } // 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다 for (const std::string& line : mirrored) { std::cout << std::string(width - line.size(), ' ') << line << '\n'; }}
Cmain.c · 35 lines
#include <stdio.h>#include <string.h>/* 짝이 있는 글자는 짝으로, 없는 글자는 그대로 돌려준다 */static char mirror(char ch) { static const char pairs[] = "()[]{}<>/\\bdpq"; /* 두 글자씩 짝 */ const char *p = strchr(pairs, ch); if (p == NULL || ch == '\0') return ch; int i = (int)(p - pairs); return i % 2 == 0 ? pairs[i + 1] : pairs[i - 1];}int main(void) { int n, width = 0; static char lines[50][256]; char buf[256]; fgets(buf, sizeof buf, stdin); sscanf(buf, "%d", &n); for (int k = 0; k < n; k++) { char *line = lines[k]; fgets(line, sizeof lines[k], stdin); line[strcspn(line, "\r\n")] = '\0'; int len = (int)strlen(line); /* C에는 문자열 바꾸기가 없어서, 양 끝에서 맞바꾸며 뒤집을 때 짝 글자로도 바꾼다 */ for (int i = 0, j = len - 1; i <= j; i++, j--) { char a = line[i], b = line[j]; line[i] = mirror(b); line[j] = mirror(a); } if (len > width) width = len; } /* 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다: %*s는 너비를 받아 앞을 빈칸으로 채운다 */ for (int k = 0; k < n; k++) printf("%*s\n", width, lines[k]); return 0;}
Craykonmain.cray · 24 lines
맡김 = Str.FromCode(1); // 입력에 나오지 않는 글자(입력은 코드 32~126만 쓴다)를 잠깐 맡아 두는 자리로 쓴다짝 = ["(", ")", "[", "]", "{", "}", "<", ">", "/", "\\", "b", "d", "p", "q"]; // 두 개씩 짝n = Console.ReadLine().ToNumber();거울 = [];너비 = 0;for (k = 0; k < n; k += 1){ s = Console.ReadLine().Reverse(); // 좌우를 뒤집는다(줄의 차례는 그대로) for (i = 0; i < 짝.Count; i += 2) { // a → b, b → a를 차례로 하면 둘 다 a가 되므로 잠깐 맡김 글자에 맡긴다 s = s.Replace(짝[i], 맡김).Replace(짝[i + 1], 짝[i]).Replace(맡김, 짝[i + 1]); } 거울.Add(s); if (s.Length > 너비) { 너비 = s.Length; }}// 왼쪽 맞춤이 뒤집히면 오른쪽 맞춤이 된다foreach (줄 in 거울){ Console.WriteLine(줄.PadLeft(너비));}