1
+ #include < iostream>
2
+ #include < algorithm>
3
+ #include < vector>
4
+ #include < map>
5
+
6
+ using namespace std ;
7
+
8
+ map<char , bool > vowels; // 모음 저장 맵
9
+
10
+ // 조합 임시배열로 문자열을 만드는 함수
11
+ string charToString (int c, vector<char > &alphabet, vector<bool > &is_in_password) {
12
+ string result = " " ;
13
+ for (int i = 0 ; i < c; i++) {
14
+ if (is_in_password[i]) { // 연산에 포함된 문자라면 result에 더하기
15
+ result += alphabet[i];
16
+ }
17
+ }
18
+ return result;
19
+ }
20
+
21
+ // 암호 만들기 가능한지 확인하는 함수 (최소 모음 1개, 최소 자음 2개)
22
+ bool isValid (int l, int c, vector<char > &alphabet, vector<bool > &is_in_password) {
23
+ int cnt_vowels = 0 ;
24
+ // 자음 개수는 암호 전체 길이에서 모음 개수를 빼서 알 수 있음
25
+ for (int i = 0 ; i < c; i++) {
26
+ if (!is_in_password[i]) { // 암호에 속하지 않았다면
27
+ continue ;
28
+ }
29
+ if (vowels[alphabet[i]]) { // 모음이라면
30
+ cnt_vowels++;
31
+ }
32
+ }
33
+ return (cnt_vowels >= 1 ) && ((l - cnt_vowels) >= 2 );
34
+ }
35
+
36
+ // 조합 구하기
37
+ vector<string> findPassword (int l, int c, vector<char > &alphabet) {
38
+ vector<string> result;
39
+ vector<bool > is_in_password (c, false );
40
+ for (int i = 0 ; i < l; i++) { // 미리 l개의 true 저장
41
+ is_in_password[i] = true ;
42
+ }
43
+ do {
44
+ if (isValid (l, c, alphabet, is_in_password)) { // 암호 만들기 가능하다면
45
+ result.push_back (charToString (c, alphabet, is_in_password));
46
+ }
47
+ } while (prev_permutation (is_in_password.begin (), is_in_password.end ()));
48
+
49
+ return result;
50
+ }
51
+
52
+ /* *
53
+ * [암호 만들기]
54
+ * 알파벳은 최대 15개 -> 브루트포스 가능
55
+ * 가능한 모든 조합 만들어서, 검사 통과하면 출력
56
+ */
57
+
58
+ int main () {
59
+ int l, c;
60
+
61
+ // 모음 저장
62
+ vowels[' a' ] = vowels[' e' ] = vowels[' i' ] = vowels[' o' ] = vowels[' u' ] = true ;
63
+
64
+ // 입력
65
+ cin >> l >> c;
66
+ vector<char > alphabet (c, 0 );
67
+ for (int i = 0 ; i < c; i++) {
68
+ cin >> alphabet[i];
69
+ }
70
+
71
+ // 연산
72
+ sort (alphabet.begin (), alphabet.end ()); // 오름차순 정렬
73
+ vector<string> result = findPassword (l, c, alphabet);
74
+
75
+ // 출력
76
+ for (int i = 0 ; i < result.size (); i++) {
77
+ cout << result[i] << ' \n ' ;
78
+ }
79
+ return 0 ;
80
+ }
0 commit comments