프로그래밍 공부/백준 (C++)
[C++ / 백준 1759번] 암호 만들기
Rocketbabydolls
2025. 1. 5. 19:23
재귀, 백트래킹, 조합, 을 통해 풀이했다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int L, C;
int vowel = 0;
int consonant = 0;
vector<char> s;
char ans[15];
void recur(int depth, int next)
{
if (depth == L)
{
vowel = 0;
consonant = 0;
for (int i = 0; i < L; i++)
{
if (ans[i] == 'a' ||
ans[i] == 'e' ||
ans[i] == 'i' ||
ans[i] == 'o' ||
ans[i] == 'u')
{
vowel++;
}
else
{
consonant++;
}
}
if (vowel < 1 || consonant < 2)
{
return;
}
for (int i = 0; i < L; i++)
{
cout << ans[i];
}
cout << '\n';
return;
}
for (int i = next; i < C; i++)
{
ans[depth] = s[i];
recur(depth + 1, i + 1);
}
}
int main()
{
cin >> L >> C;
char input;
for (int i = 0; i < C; i++)
{
cin >> input;
s.push_back(input);
}
sort(s.begin(), s.end());
recur(0,0);
return 0;
}