SMALL

문제 링크
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
DFS를 활용하는 문제, 우선 문자들을 모두 받아, 오름차순으로 정렬한 후에 DFS를 시작한다. Vector의 사이즈가 L에 도달할 때만 출력에 대해 검증(자음 2개이상, 모음 1개 이상)하고, 출력한다. 다음 재귀는 +1을 해서 호출한다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
int L, C;
vector<char> vec;
vector<char> tmpAns;
void DFS(int depth){
int vali = false;
int consonant = 0;
int vowel = 0;
int passwordSize = (int)tmpAns.size();
if (passwordSize == L) {
for (int i = 0; i < L; i++) {
if (tmpAns[i] == 'a' || tmpAns[i] == 'e' || tmpAns[i] == 'i' || tmpAns[i] == 'o' || tmpAns[i] == 'u') {
vowel += 1;
}
else {
consonant += 1;
}
}
if (consonant >= 2 && vowel >= 1) {
vali = true;
}
else {
vali = false;
}
if (vali) {
for (int j = 0; j < L; j++) {
cout << tmpAns[j];
}
cout << '\n';
return;
}
}
for (int t = depth; t < C; t++) {
tmpAns.push_back(vec[t]);
DFS(t + 1);
tmpAns.pop_back();
}
return;
}
int main()
{
cin >> L;
cin >> C;
for (int i = 0; i < C; i++) {
char tmp;
cin >> tmp;
vec.push_back(tmp);
}
sort(vec.begin(), vec.end());
DFS(0);
return 0;
}
LIST
'BOJ' 카테고리의 다른 글
2022 KAKAO TECH INTERNSHIP : 성격 유형 검사하기 (2) | 2023.11.13 |
---|---|
2023 KAKAO BLIND RECRUITMENT : 개인정보 수집 유효기간 (0) | 2023.11.08 |
BOJ 1157번 단어공부 파이썬 (0) | 2022.03.17 |
백준 9012번 괄호 자바 풀이 (0) | 2022.01.19 |
BOJ 17608번 막대기 (0) | 2022.01.13 |