본문 바로가기

Problem Solving/BOJ

[BOJ] 1759 : 암호 만들기

처음에 함수 범위를 잘못 지정해서 3번이나 틀렸었다...



간단한 조합 문제이다.


우선 주어진 알파벳을 정렬하고, L개의 서로 다른 알파벳을 조합으로 선택한다.


이런 순열, 조합 문제는 백준에 있는 N과 M 시리즈에서 기초를 다질 수 있다.


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
#include <bits/stdc++.h>
using namespace std;
 
char arr[15];
char ans[15];
int l, c;
void solve(int depth, int before) {
    if (depth == l) {
        int sum = 0;
        for (int i = 0; i < l; i++) {
            if (ans[i] == 'a' || ans[i] == 'e' || ans[i] == 'i' || ans[i] == 'o' || ans[i] == 'u') {
                sum++;
            }
        }
        if (sum >= 1 && l - sum >= 2) {
            for (int i = 0; i < l; i++cout << ans[i];
            cout << '\n';
        }
        return;
    }
 
    for (int i = before; i <= c - (l - depth); i++) {
        ans[depth] = arr[i];
        solve(depth + 1, i + 1);
    }
}
 
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> l >> c;
    for (int i = 0; i < c; i++cin >> arr[i];
    sort(arr, arr + c);
    solve(00);
}
cs


'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 2225 : 합분해  (0) 2018.12.30
[BOJ] 10171 : 고양이  (0) 2018.12.30
[BOJ] 15666 : N과 M (12)  (0) 2018.12.30
[BOJ] 15665 : N과 M (11)  (0) 2018.12.30
[BOJ] 15664 : N과 M (10)  (0) 2018.12.30