GitHubSeob
C++ / 백준 / 1759 / 암호 만들기 본문
문제
https://www.acmicpc.net/problem/1759
1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
문제풀이
DFS가 아닌 비트마스크를 이용하여 문제를 풀었다.
글자를 모두 입력받고 정렬한다.
모든 경우의 수를 돌면서 해당 글자가 자음인지, 모음인지를 확인하고, 조건들을 만족했을 때 set인 answer에 insert 했다.
반복이 종료되면 set의 원소를 모두 출력한다.
코드
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string word = "";
string code = "";
int L(0), C(0);
int consonant(0), vowel(0);
int idx(0), idx2(0);
set<string>answer;
set<string>::iterator it;
cin >> L >> C;
char character = ' ';
for (idx = 0; idx < C; ++idx) {
cin >> character;
word += character;
}
sort(word.begin(), word.end());
for (idx = 1; idx < (1 << C); ++idx) {
consonant = 0;
vowel = 0;
code = "";
for (idx2 = 0; idx2 < C; ++idx2) {
if (idx & (1 << idx2)) {
if (word[idx2] == 'a' || word[idx2] == 'e' || word[idx2] == 'i' || word[idx2] == 'o' || word[idx2] == 'u')
++vowel;
else ++consonant;
code += word[idx2];
}
}
if (vowel >= 1 && consonant >= 2 && vowel + consonant == L)
answer.insert(code);
}
for (it = answer.begin(); it != answer.end(); ++it)
cout << *it << '\n';
}
'Baekjoon > Gold' 카테고리의 다른 글
C++ / 백준 / 1987 / 알파벳 (0) | 2021.09.30 |
---|---|
C++ / 백준 / 2580 / 스도쿠 (0) | 2021.09.30 |
C++ / 백준 / 5014 / 스타트링크 (0) | 2021.09.26 |
C++ / 백준 / 3108 / 로고 (0) | 2021.09.24 |
C++ / 백준 / 2186 / 문자판 (0) | 2021.09.23 |