풀이
조합을 통해 문제에서 요구하는 길이만큼의 암호가 만들어졌다면 문제에서 주어진 조건에 부합하는지 판별하고 정답에 추가함으로써 해결했습니다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static int L, C;
public static StringBuilder sb;
public static char[] words;
public static int[] number;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
L = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
words = new char[C];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < C; ++i) {
words[i] = st.nextToken().charAt(0);
}
// 암호를 이루는 알파벳이 정렬되어 증가하는 순서로 배열되있을 것으로 추측되므로
// 우선 정렬을 해준다.
Arrays.sort(words);
sb = new StringBuilder("");
combination(0, 0, "");
System.out.println(sb);
}
public static void combination(int cnt, int idx, String s) {
// 주어진 길이만큼 암호가 만들어지면
if (cnt == L) {
// 모음은 1개 이상
int vowelCnt = 0;
// 자음을 2개 이상
int consonantsCnt = 0;
for (int i = 0; i < L; ++i) {
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') {
vowelCnt++;
}
else {
consonantsCnt++;
}
}
// 모음, 자음 조건 체크
// 증가하는 순서는 이미 원본 알파벳들을 순서대로 정렬했으므로 상관 X
if (vowelCnt >= 1 && consonantsCnt >= 2) {
sb.append(s).append("\n");
}
return;
}
// 알파벳 암호 조합을 찾는다.
for (int i = idx; i < C; ++i) {
combination(cnt + 1, i + 1, s + Character.toString(words[i]));
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[Java] BOJ 14500번 테트로미노 (0) | 2021.05.13 |
---|---|
[Java] BOJ 15686번 치킨 배달 (0) | 2021.05.12 |
[Java] BOJ 14503번 로봇 청소기 (0) | 2021.05.10 |
[Java] BOJ 11722번 가장 긴 감소하는 부분 수열 (0) | 2021.05.07 |
[Java] BOJ 11055번 가장 큰 증가 부분 수열 (0) | 2021.05.06 |