-
[1759] 암호 만들기BOJ 2021. 9. 13. 18:24
https://www.acmicpc.net/problem/1759
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
<문제>
N과 M문제와 매우 유사한 문제다. 완전탐색(백트래킹)으로 해결할 수 있다.
<소스코드>
123456789101112131415161718192021222324252627282930313233343536373839#include<stdio.h>#include<algorithm>using namespace std;int m, n;int a[20], b[20];bool check[20];void f(int depth, int idx, int cnt, int cnt2) {//깊이, 인덱스, 모음, 자음if (depth == m) {if (cnt < 1 || cnt2 < 2)return;for (int x = 0; x < depth; x++) {printf("%c", b[x]);}printf("\n");return;}for (int i = idx; i < n; i++) {if (check[i] == false) {check[i] = true;b[depth] = a[i];if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') {f(depth + 1, i + 1, cnt + 1, cnt2);}else {f(depth + 1, i + 1, cnt, cnt2 + 1);}check[i] = false;}}}int main(void) {int i;scanf("%d %d", &m, &n);getchar();for (i = 0; i < n; i++)scanf(" %c", &a[i]);sort(a, a + n);f(0, 0, 0, 0);return 0;}cs 'BOJ' 카테고리의 다른 글
[1939] 중량제한 (0) 2021.09.17 [14502] 연구소 (0) 2021.09.15 [1197] 최소 스패닝 트리 (0) 2021.09.13 [1753] 최단경로 (0) 2021.09.13 [11508] 2+1 세일 (0) 2021.09.12