Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- cs
- 컴퓨터공학과
- 컴공과
- Computer science
- 오퍼레이팅시스템
- vector
- bfs
- 문제풀이
- 스택
- 오에스
- 백준
- 코테
- coding
- 구현
- 코딩
- 자료구조
- Stack
- OS
- 북리뷰
- 알고리즘
- Operating System
- c++
- 정석학술정보관
- 그래프
- 너비우선탐색
- 컴공
- DP
- 브루트포스
- 개발
- 정석
Archives
- Today
- Total
Little Jay
[C++] 백준 1759번 - 암호 만들기 본문
오랜만에 풀어본 Brute Force 문제
C++에는 순열을 쉽게 만들 수 있는 algorithm 헤더에 next_permutation(혹은 prev_permutation) 메서드가 존재한다.
그러나 우리가 풀고자 하는 것은 조합을 만들어야 하는 것이기 때문에 1의 개수를 L개 만큼, 그리고 0의 개수를 C-L개 만큼 넣어준다음에 해당 벡터에 1이 나온다면 출력하는 식으로 조합을 만들 수 있다.
그렇게 Possible한 String을 만들었다면 vowel과 noun의 개수가 맞는지 체크하고 출력해주면 되는 문제이다.
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int l, c;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> l >> c;
vector<string> v(c);
for (int i = 0; i < c; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
vector<int> temp_v;
for (int i = 0; i < l; i++) {
temp_v.push_back(1);
}
for (int i = 0; i < v.size() - l; i++) {
temp_v.push_back(0);
}
sort(temp_v.begin(), temp_v.end());
vector<string> ans;
string pwd = "";
do {
for (int i = 0; i < temp_v.size(); i++) {
if (temp_v[i] == 1) {
pwd += v[i];
}
}
ans.push_back(pwd);
pwd = "";
} while (next_permutation(temp_v.begin(), temp_v.end()));
for (int i = ans.size() - 1; i >= 0; i--) {
int vowel = 0, noun = 0;
for (int j = 0; j < l; j++) {
if (ans[i][j] == 'a' || ans[i][j] == 'e' || ans[i][j] == 'i' || ans[i][j] == 'o' || ans[i][j] == 'u') vowel++;
else noun++;
}
if (vowel >= 1 && noun >= 2)
cout << ans[i] << endl;
}
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 10026번 - 적녹색약 (0) | 2022.08.31 |
---|---|
[C++] 백준 1965번 - 상자 넣기 (0) | 2022.08.30 |
[C++] 백준 2206번 - 벽 부수고 이동하기 (0) | 2022.08.23 |
[C++] 백준 20301번 - 반전 요세푸스 (0) | 2022.08.05 |
[C++] 백준 13273번 - 로마숫자 (0) | 2022.07.30 |
Comments