Search
moon
sun
📒

암호 만들기

주차
문제번호
1759
언어
티어
골드
유형
백트래킹
수학
조합론
브루트포스
nj_Blog
nj_상태
이해도
66%
풀이
사람
이해도 2
13 more properties

문제접근🤔

•
입력 받은 알파벳을 정렬을 하여 순서대로 체크하기 편하게 한다.
•
모든 알파벳에 대해서 하나씩 대입을 해나간다
•
단, 두 개의 경우로 나누어서 생각을 해야한다(둘다 모두 체크를 해줘야함)
1.
들어갈 알파벳이 모음인 경우
2.
들어갈 알파벳이 자음인 경우
•
재귀를 탈때마다 정답으로 출력할 알파벳 길이를 체크하고 만족한다면, 모음 개수와 자음 개수도 조건을 만족하는지 체크하고 출력한다

놓쳤던 부분😅

코드😁

2016 KB

0 ms

#include <iostream> #include <algorithm> int l, c; char input_arr[15]; char answer[15]; void input_setting() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); } void input() { std::cin >> l >> c; for (int i = 0; i < c; i++) std::cin >> input_arr[i]; std::sort(input_arr, input_arr + c); } bool isVowel(int idx) { if (input_arr[idx] == 'a' || input_arr[idx] == 'e' || input_arr[idx] == 'i' || input_arr[idx] == 'o' || input_arr[idx] == 'u') return (1); return (0); } void solution(int answer_index, int pre_index, int vowel, int consonant) { if (answer_index == l) { if (vowel >= 1 && consonant >= 2) { for (int i = 0; i < l; i++) std::cout << answer[i]; std::cout << "\n"; return ; } } for (int i = pre_index + 1; i < c; i++) { answer[answer_index] = input_arr[i]; if (isVowel(i)) solution(answer_index + 1, i, vowel + 1, consonant); else solution(answer_index + 1, i, vowel, consonant + 1); } } int main(void) { input_setting(); input(); solution(0, -1, 0, 0); return (0); }
C++
복사