본문 바로가기
알고리즘/백트래킹

[백준 풀이_Java] 1759 암호 만들기 (골드5)

by happyhelen 2024. 2. 19.

 

이제는 변형 문제도 풀 수 있게 되었다! 신나~!

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;

public class Main {

    static int L;
    static int C;
    static int[] arrAnswer;
    static String[] arrAbc;
    static int cntAeiou;
    static int cntElse;
    static StringBuilder sb = new StringBuilder();
    static String strAeiou = "aeiou";

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        L = Integer.parseInt(input[0]);
        C = Integer.parseInt(input[1]);
        arrAnswer = new int[L];

        arrAbc = br.readLine().split(" ");
        Arrays.sort(arrAbc);

        recur( 0, 0);

        System.out.println(sb);
    }

    static void recur(int start, int depth){
        StringBuilder sbTmp;
        
        if(depth == L){ // L개라면

            sbTmp = new StringBuilder();
            cntAeiou = 0;
            cntElse = 0;

            for(int i : arrAnswer){
                sbTmp.append(arrAbc[i]);

                if(strAeiou.contains(arrAbc[i])){ // 자모음 카운트
                    cntAeiou++;
                }else{
                    cntElse++;
                }
            }


            if(cntAeiou > 0 && cntElse > 1){
                sb.append(sbTmp);
                sb.append("\n");
            }
            return;
        }

        for(int i=start; i<C; i++){
            arrAnswer[depth] = i;
            recur(i+1,depth + 1);
        }
    }
}