시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 128 MB | 43108 | 20321 | 14101 | 44.803% |
바로 어제 최백준 조교가 방 열쇠를 주머니에 넣은 채 깜빡하고 서울로 가 버리는 황당한 상황에 직면한 조교들은, 702호에 새로운 보안 시스템을 설치하기로 하였다. 이 보안 시스템은 열쇠가 아닌 암호로 동작하게 되어 있는 시스템이다.
암호는 서로 다른 L개의 알파벳 소문자들로 구성되며 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음으로 구성되어 있다고 알려져 있다. 또한 정렬된 문자열을 선호하는 조교들의 성향으로 미루어 보아 암호를 이루는 알파벳이 암호에서 증가하는 순서로 배열되었을 것이라고 추측된다. 즉, abc는 가능성이 있는 암호이지만 bac는 그렇지 않다.
새 보안 시스템에서 조교들이 암호로 사용했을 법한 문자의 종류는 C가지가 있다고 한다. 이 알파벳을 입수한 민식, 영식 형제는 조교들의 방에 침투하기 위해 암호를 추측해 보려고 한다. C개의 문자들이 모두 주어졌을 때, 가능성 있는 암호들을 모두 구하는 프로그램을 작성하시오.
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
각 줄에 하나씩, 사전식으로 가능성 있는 암호를 모두 출력한다.
4 6
a t c i s w
acis
acit
aciw
acst
acsw
actw
aist
aisw
aitw
astw
cist
cisw
citw
istw
import java.io.*;
import java.util.Arrays;
public class P_1759 {
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int[] info;
static String[] alpha;
static String[] password;
static int consonant = 0;
static int vowel = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
info = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
alpha = br.readLine().split(" ");
password = new String[info[0]];
Arrays.sort(alpha);
find_password(0);
}
public static void find_password(int pos) throws IOException {
if (pos == info[0]) {
consonant = vowel = 0;
for (String elem : password) {
if (elem.equals("a") || elem.equals("e") || elem.equals("i") || elem.equals("o") || elem.equals("u"))
consonant++;
else
vowel++;
}
if (consonant >= 1 && vowel >= 2) {
for (String elem : password) bw.write(elem);
bw.newLine();
bw.flush();
}
}
else {
for (int i = pos; i < info[1]; i++) {
password[pos] = alpha[i];
if (check_redundancy(pos))
password[pos] = null;
else {
find_password(pos + 1);
}
}
}
}
public static boolean check_redundancy(int pos) {
for (int i = 0; i < pos; i++) {
if (password[i].compareTo(password[pos]) > 0) return true;
if (password[i].equals(password[pos])) return true;
}
return false;
}
}
백트래킹을 이용해서 password 배열을 채워준다.
check_redundancy 함수에서 중복과 오름차순 조건을 확인하고 재귀 함수의 중단 조건에 자음과 모음 개수를 확인한다.