한글 프로그램의 메뉴에는 총 N개의 옵션이 있다. 각 옵션들은 한 개 또는 여러 개의 단어로 옵션의 기능을 설명하여 놓았다. 그리고 우리는 위에서부터 차례대로 각 옵션에 단축키를 의미하는 대표 알파벳을 지정하기로 하였다. 단축키를 지정하는 법은 아래의 순서를 따른다.
1. 먼저 하나의 옵션에 대해 왼쪽에서부터 오른쪽 순서로 단어의 첫 글자가 이미 단축키로 지정되었는지 살펴본다. 만약 단축키로 아직 지정이 안 되어있다면 그 알파벳을 단축키로 지정한다.
2. 만약 모든 단어의 첫 글자가 이미 지정이 되어있다면 왼쪽에서부터 차례대로 알파벳을 보면서 단축키로 지정 안 된 것이 있다면 단축키로 지정한다.
3. 어떠한 것도 단축키로 지정할 수 없다면 그냥 놔두며 대소문자를 구분치 않는다.
4. 위의 규칙을 첫 번째 옵션부터 N번째 옵션까지 차례대로 적용한다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Set<String> set = new HashSet<>();
for (int i = 0; i < N; i++) {
String temp = br.readLine();
String[] tempArr = temp.split(" ");
boolean cond1 = true;
StringBuilder answer = new StringBuilder();
for (String s : tempArr) {
String first = s.charAt(0) + "";
if (set.contains(first.toUpperCase()) || !cond1) {
answer.append(s).append(" ");
} else {
cond1 = false;
set.add(first.toUpperCase());
answer.append("[").append(first).append("]").append(s.substring(1)).append(" ");
}
}
if (cond1) {
boolean cond2 = true;
StringBuilder answer2 = new StringBuilder();
for (int j = 0; j < temp.length(); j++) {
String index = temp.charAt(j) + "";
if (index.equals(" ")) {
answer2.append(index);
continue;
}
if (set.contains(index.toUpperCase()) || !cond2) {
answer2.append(index);
} else {
cond2 = false;
set.add(index.toUpperCase());
answer2.append("[").append(index).append("]");
}
}
if (cond2) {
System.out.println(temp);
} else {
System.out.println(answer2);
}
} else {
System.out.println(answer);
}
}
}
}