[BOJ 1759] 암호 만들기
🔗Link
💡Idea
암호는 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음으로 구성되어 있음을 주의하고, 모든 경우의 수를 구해주면 되겠다.
🔑Code
#include <bits/stdc++.h>
using namespace std;
int L, C;
vector<char> spell;
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> L >> C;
for (int i = 0; i < C; i++) {
char c;
cin >> c;
spell.emplace_back(c);
}
sort(spell.begin(), spell.end());
vector<bool>isUnselected(L, 0);
isUnselected.resize(C, 1);
do {
bool hasMother = false;
int hasSon = 0;
// 단어 생성
string pw = "";
for (int i = 0; i < spell.size(); i++) {
if (isUnselected[i]) continue;
// 자모음 확인
if (spell[i] == 'a' || spell[i] == 'e' || spell[i] == 'i' || spell[i] == 'o' || spell[i] == 'u')
hasMother = true;
else
hasSon++;
pw += spell[i];
}
if (hasMother && hasSon >= 2) cout << pw << '\n';
} while (next_permutation(isUnselected.begin(), isUnselected.end()));
return 0;
}
🗨️ Side Notes
조합 문제는 재귀로 풀어도 좋지만, next_permutation()을 쓰면 구현이 쉽다. 다음 링크 참고 (조합 구현 팁)