|
| 1 | +// https://www.acmicpc.net/problem/1283 |
| 2 | +const fs = require("fs"); |
| 3 | +const filePath = process.platform === "linux" ? "/dev/stdin" : "./서정우/input.txt"; |
| 4 | +const input = fs.readFileSync(filePath).toString().trim().split("\n"); |
| 5 | + |
| 6 | +const N = Number(input[0]); |
| 7 | +const lines = input.slice(1); |
| 8 | +const keymap = new Map(); |
| 9 | +const result = []; |
| 10 | + |
| 11 | +const wrappingWithBrackets = (str, index) => { |
| 12 | + return str.slice(0, index) + "[" + str.slice(index, index + 1) + "]" + str.slice(index + 1); |
| 13 | +}; |
| 14 | + |
| 15 | +for (let i = 0; i < N; i++) { |
| 16 | + const word = lines[i].split(" "); |
| 17 | + let mapString = ""; |
| 18 | + let isMapped = false; |
| 19 | + |
| 20 | + for (let j = 0; j < word.length; j++) { |
| 21 | + const cap = word[j][0].toUpperCase(); |
| 22 | + |
| 23 | + if (!isMapped && !keymap.get(cap)) { |
| 24 | + keymap.set(cap, 1); |
| 25 | + isMapped = true; |
| 26 | + mapString += |
| 27 | + j < word.length - 1 |
| 28 | + ? wrappingWithBrackets(word[j], 0) + " " |
| 29 | + : wrappingWithBrackets(word[j], 0); |
| 30 | + } else { |
| 31 | + mapString += j < word.length - 1 ? word[j] + " " : word[j]; |
| 32 | + continue; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (mapString.length === lines[i].length) { |
| 37 | + for (let k = 0; k < lines[i].length; k++) { |
| 38 | + if (lines[i][k] !== " " && !keymap.get(lines[i][k].toUpperCase())) { |
| 39 | + keymap.set(lines[i][k].toUpperCase(), 1); |
| 40 | + result.push(wrappingWithBrackets(lines[i], k)); |
| 41 | + break; |
| 42 | + } else if (k === lines[i].length - 1) { |
| 43 | + result.push(lines[i]); |
| 44 | + } |
| 45 | + } |
| 46 | + } else { |
| 47 | + result.push(mapString); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +console.log(result.join("\n")); |
0 commit comments