https://www.acmicpc.net/problem/1759

 

1759번: 암호 만들기

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

www.acmicpc.net

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	static int L, C;
	static char[] ch, isSelected;
	static StringBuilder sb = new StringBuilder();

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

		L = Integer.parseInt(st.nextToken()); // 암호 문자 개수
		C = Integer.parseInt(st.nextToken()); // 주어지는 문자

		ch = new char[C];
		isSelected = new char[L];
		ch = br.readLine().replace(" ", "").toCharArray();

		Arrays.sort(ch); // 오름차순으로 정렬
		combination(0, 0);
		System.out.println(sb);
	}

	private static void combination(int cnt, int start) {
		if (cnt == L) {
			int cntVowel = 0; // 모음 개수
			int cntConsonant = 0; // 자음 개수

			for (int i = 0; i < L; i++) {
				if (isSelected[i] == 'a' || isSelected[i] == 'e' || isSelected[i] == 'i' || isSelected[i] == 'o'
						|| isSelected[i] == 'u') {
					cntVowel++;
				} else {
					cntConsonant++;
				}
			}
            
            // 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음을 갖는 조건 만족하는지 확인
			if (cntVowel >= 1 && cntConsonant >= 2) {
				for (int i = 0; i < L; i++) {
					sb.append(isSelected[i]);
				}
				sb.append("\n");
			}
			return;
		}

		for (int i = start; i < C; i++) {
			isSelected[cnt] = ch[i];
			combination(cnt + 1, i + 1);
		}
	}
}

 

+ Recent posts