Algorithm/solution
#1759. 암호 만들기
silverbell
2017. 12. 29. 17:20
https://www.acmicpc.net/problem/1759
1. 문제 이해
모음 1개와 자음 2개 이상이 꼭 포함 된다는 것을 기억해야 합니다.
2. 접근 방법
로또 (http://sbell92.tistory.com/15?category=640303) 문제와 비슷한 방법으로 접근했습니다.
dfs를 이용해 완전 탐색을 하되, 모음과 자음일 경우를 고려해 주었습니다.
3. 문제 해결
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <algorithm> #include <iostream> #include <vector> #define MAX_SIZE 20 using namespace std; int l, c, ja, mo; vector<char> res; void dfs(vector<char>& v, int d, int cnt) { if (l == cnt && mo >= 1 && ja >= 2) { for (int i = 0; i < res.size(); i++) { cout << res[i]; } cout << '\n'; return; } if (l == cnt) return; // 모음, 자음 만족 안하고 길이가 l이 될때 if (c == d) return; // 완전히 탐색했을때 if (v[d] == 'a' || v[d] == 'e' || v[d] == 'i' || v[d] == 'o' || v[d] == 'u') { mo++; } else { ja++; } res.push_back(v[d]); dfs(v, d + 1, cnt + 1); char out = res.back(); if (out == 'a' || out == 'e' || out == 'i' || out == 'o' || out == 'u') { mo--; } else { ja--; } res.pop_back(); dfs(v, d + 1, cnt); } int main() { cin >> l >> c; char input; vector<char> v; for (int i = 0; i < c; i++) { cin >> input; v.push_back(input); } sort(v.begin(), v.end()); dfs(v, 0, 0); return 0; } | cs |
※ 오늘의 코드 리뷰 : 벡터가 점점 익숙해져 갑니다. ㅎㅎ