문제풀이/백준oj

[백준oj] 1759번 암호 만들기

Hyeon-Uk 2020. 12. 21. 13:28
반응형

www.acmicpc.net/problem/1759

 

1759번: 암호 만들기

첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.

www.acmicpc.net


 

이문제는 dfs로 해결을 하면된다. dfs로 해결을 하지만, 마지막 출력시 모음이 최소1개있는지, 자음이 최소2개는 있는지까지 체크를 해줘야한다.

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

char arr[15];//입력받는 문자 저장배열
int check[15] = { 0 };//방문유무
int l, c;
int mo=0, ja=0;//모음 자음
char result[15];//출력 결과물

//모음인지 자음인지 체크해주는 함수
bool ismo(char c) {
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
		return true;
	}
	else {
		return false;
	}
}

void dfs(int num,int cnt) {
	if (mo >= 1 && ja >= 2 && cnt == l) {
		for (int i = 0; i < l; i++) {
			cout << result[i];
		}
		cout << "\n";
		return;
	}

	for (int i = num; i < c; i++) {
		if (check[i] == false) {
			check[i] = true;
			result[cnt] = arr[i];
			if (ismo(arr[i])) {
				mo++;
			}
			else {
				ja++;
			}
			dfs(i+1,cnt + 1);
			if (ismo(arr[i])) {
				mo--;
			}
			else {
				ja--;
			}
			check[i] = false;
		}
	}
}

int main() {
	cin >> l >> c;

	for (int i = 0; i < c; i++) {
		cin >> arr[i];
	}

	sort(arr+0, arr+c);
	dfs(0,0);
	return 0;
}
반응형