난이도: Gold5
문제) 바로 어제 최백준 조교가 방 열쇠를 주머니에 넣은 채 깜빡하고 서울로 가 버리는 황당한 상황에 직면한 조교들은, 702호에 새로운 보안 시스템을 설치하기로 하였다. 이 보안 시스템은 열쇠가 아닌 암호로 동작하게 되어 있는 시스템이다.
암호는 서로 다른 L개의 알파벳 소문자들로 구성되며 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음으로 구성되어 있다고 알려져 있다. 또한 정렬된 문자열을 선호하는 조교들의 성향으로 미루어 보아 암호를 이루는 알파벳이 암호에서 증가하는 순서로 배열되었을 것이라고 추측된다. 즉, abc는 가능성이 있는 암호이지만 bac는 그렇지 않다.
새 보안 시스템에서 조교들이 암호로 사용했을 법한 문자의 종류는 C가지가 있다고 한다. 이 알파벳을 입수한 민식, 영식 형제는 조교들의 방에 침투하기 위해 암호를 추측해 보려고 한다. C개의 문자들이 모두 주어졌을 때, 가능성 있는 암호들을 모두 구하는 프로그램을 작성하시오.
package algorithm_lab.day13.hw;
import java.util.Arrays;
import java.util.Scanner;
public class BJ_1759 {
static int L,C;
static String[] str,s;
static StringBuilder sb;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sb = new StringBuilder();
L = sc.nextInt();
C = sc.nextInt();
s = new String[C];
str = new String[L];
for(int i=0;i<C;i++) {
s[i]=sc.next();
}
Arrays.sort(s);
perm(0,0);
System.out.println(sb.toString());
}
public static void perm(int cnt, int start) {
if(cnt==L) {
int count=0;
int extra=0;
for(int i=0;i<L;i++) {
if(str[i].equals("a")||str[i].equals("e")||str[i].equals("i")||str[i].equals("o")||str[i].equals("u")) count++;
else extra++;
if(count>=1&&extra>=2) break;
}
if(count>=1&&extra>=2) {
for(int i=0;i<L;i++) {
sb.append(str[i]);
}
sb.append("\n");
}
return;
}
for(int i=start;i<C;i++) {
str[cnt]=s[i];
perm(cnt+1, i+1);
}
}
}