BJ1759 암호 만들기

·2022년 4월 17일
0

백준 알고리즘

목록 보기
14/34

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

이 문제는 조건에 맞는 모든 암호를 출력하는 것이다.

사전식으로 출력해야 하므로 A~Z순으로 정렬해주고, 문자에 중복이 없으므로 조합을 재귀함수로 구현하여 길이가 맞을 때
출력하면 쉽게 해결할 수 있다.

package day0221;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class MakePassword {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	static StringTokenizer st;
	static int L, C;
	static char[] input, number;

	static void combi(int count, int start, int numofA, int numofB) throws IOException {
		if (count == L) {
			if (numofA >= 1 && numofB >= 2) {
				for(char c : number) {
					bw.append(c);
				}
				bw.append("\n");
				return;
			} else {
				return;
			}
		}

		for (int i = start; i < C; i++) {
			number[count] = input[i];
			if (input[i] == 'a' || input[i] == 'e' || input[i] == 'i' || input[i] == 'o' || input[i] == 'u') {
				combi(count + 1, i + 1, numofA + 1, numofB);
			} else {
				combi(count + 1, i + 1, numofA, numofB + 1);
			}

		}

	}

	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(br.readLine(), " ");
		L = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		input = new char[C];
		number = new char[L];

		st = new StringTokenizer(br.readLine(), " ");
		for (int i = 0; i < C; i++) {
			input[i] = st.nextToken().charAt(0);
		}
		Arrays.sort(input);

		combi(0, 0, 0, 0);
		bw.flush();
	}
}
profile
SSAFY 7기

0개의 댓글