[백준][1759][c++] 암호 만들기

HanGyul Moon·2021년 9월 20일

암호 만들기 문제 링크

[풀이]
조합을 통해 얻은 것이 최소한의 한개의 모음과 두개의 자음을 만족하면 저장해 두었다가 sort한 후 출력하면 된다

[코드]

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int L, C;
vector<char> alphabet;
vector<string> result;

bool check_condition(string str) {
	int u_check = 0; // 모음 개수 check
	int v_check = 0; // 자은 개수 체크
	for (int i = 0; i < str.size(); i++) {
		if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') u_check++;
		else v_check++;
	}
	
	//조건
	if (u_check >= 1 && v_check >= 2) return true;
	return false;
}

void combination(int idx, int depth, string str_result) {
	if (depth == L) {
		//조합을 통해 얻은 것이 조건을 만족하면 result vector에 넣는다
		sort(str_result.begin(), str_result.end());
		bool check = check_condition(str_result);
		if (check) {
			result.push_back(str_result);
		}
	}
	else {
		for (int i = idx; i < C; i++) {
			str_result.push_back(alphabet[i]);
			combination(i + 1, depth + 1, str_result);
			str_result.pop_back();
		}
	}
}

int main() {
	cin >> L >> C;
	
	char input;
	for (int i = 0; i < C; i++) {
		cin >> input;
		alphabet.push_back(input);
	}

	combination(0, 0, "");

	sort(result.begin(), result.end());
	for (int i = 0; i < result.size(); i++) {
		cout << result[i] << "\n";
	}
}

[총평]
쉬운 조합 문제 였다고 생각한다.

profile
시작은 미약하게...

0개의 댓글