boj)1759 - 암호 만들기
2020. 12. 1. 13:42ㆍPS/boj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import java.io.*;
import java.util.*;
public class boj_1759 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static int l, c;
static char[] a;
static char[] input;
static boolean[] v = new boolean[15];
public static void main(String[] args) throws IOException {
st = new StringTokenizer(br.readLine());
l = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
a = new char[l];
input = new char[c];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < c; i++) {
input[i] = st.nextToken().charAt(0);
}
Arrays.sort(input);
func(0, 0, 0);
bw.flush();
bw.close();
}
static void func(int k, int vowelCount, int consonantCount) throws IOException {
// base condition
if (k == l) {
if (vowelCount >= 1 && consonantCount >= 2) {
for (int i = 0; i < l; i++) {
bw.write(a[i]);
}
bw.newLine();
}
return;
}
for (int i = 0; i < c; i++) {
if (v[i]) continue;
if (k != 0 && a[k-1] > input[i]) continue;
a[k] = input[i];
v[i] = true;
boolean isVowel = isVowel(input[i]);
if (isVowel) vowelCount++;
else consonantCount++;
func(k+1, vowelCount, consonantCount);
v[i] = false;
if (isVowel) vowelCount--;
else consonantCount--;
}
}
static boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
}
|
cs |
- 백트래킹, 재귀
- N과 M 문제처럼 백트래킹으로 풀었다.
- 아니면 c 의 제한이 15 까지니까 현재의 알파벳에 대해 사용할지 말지 O, X 의 관점으로 접근할 수도 있다.
- go (int k, char[] alpha, String pass)
- alpha 배열의 k 번째 index 값을 사용할 지 말지 사용하면 pass에 추가, 아니면 pass 그대로, k + 1 로 다시 호출
'PS > boj' 카테고리의 다른 글
boj)14889 - 스타트와 링크 (0) | 2020.12.03 |
---|---|
boj)14501 - 퇴사 (0) | 2020.12.02 |
boj)6603 - 로또 (0) | 2020.12.01 |
boj)10971 - 외판원 순회 2 (0) | 2020.11.30 |
boj)10819 - 차이를 최대로 (0) | 2020.11.30 |