코딩테스트

백준 1759번 암호 만들기(자바) - 조합, dfs

leeeehhjj 2023. 2. 25. 18:01

암호 만들기 

한국어   
시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 128 MB 58409 27658 19027 44.633%

문제

바로 어제 최백준 조교가 방 열쇠를 주머니에 넣은 채 깜빡하고 서울로 가 버리는 황당한 상황에 직면한 조교들은, 702호에 새로운 보안 시스템을 설치하기로 하였다. 이 보안 시스템은 열쇠가 아닌 암호로 동작하게 되어 있는 시스템이다.

암호는 서로 다른 L개의 알파벳 소문자들로 구성되며 최소 한 개의 모음(a, e, i, o, u)과 최소 두 개의 자음으로 구성되어 있다고 알려져 있다. 또한 정렬된 문자열을 선호하는 조교들의 성향으로 미루어 보아 암호를 이루는 알파벳이 암호에서 증가하는 순서로 배열되었을 것이라고 추측된다. 즉, abc는 가능성이 있는 암호이지만 bac는 그렇지 않다.

새 보안 시스템에서 조교들이 암호로 사용했을 법한 문자의 종류는 C가지가 있다고 한다. 이 알파벳을 입수한 민식, 영식 형제는 조교들의 방에 침투하기 위해 암호를 추측해 보려고 한다. C개의 문자들이 모두 주어졌을 때, 가능성 있는 암호들을 모두 구하는 프로그램을 작성하시오.

입력

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

출력

각 줄에 하나씩, 사전식으로 가능성 있는 암호를 모두 출력한다.

예제 입력 1 복사

4 6
a t c i s w

예제 출력 1 복사

acis
acit
aciw
acst
acsw
actw
aist
aisw
aitw
astw
cist
cisw
citw
istw

import java.util.*;
class Main {
    static int l,c;
    static char[] ch;
    static boolean visited[];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        l = sc.nextInt();
        c = sc.nextInt();
        ch = new char[c];
        visited = new boolean[c];
        for (int i = 0; i < c; i++) {
            ch[i] = sc.next().charAt(0);
        }
        Arrays.sort(ch);
        dfs(0, "", 0);
    }

    static void dfs(int depth, String result, int idx) {
        if (depth == l) {
            if (check(result)) {
                System.out.println(result);
            }
            return;
        }

        for (int i = idx; i < ch.length; i++) {
            if (!visited[i]) {
                visited[i] = true;
                result += Character.toString(ch[i]);
                dfs(depth+1, result, i);
                visited[i] = false;
                result = result.substring(0,result.length()-1);

            }
        }
    }

    static boolean check(String result) {
        int m=0;
        int j=0;
        for (int i = 0; i < result.length(); i++) {
            if (result.charAt(i) == 'a' || result.charAt(i) == 'e' || result.charAt(i) == 'i' ||
                    result.charAt(i) == 'o' || result.charAt(i) == 'u') {
                m++;
            }
            else j++;
        }
        if (m > 0 && j >1) return true;
        return false;
    }
}

처음에는 visited를 사용해 체크했는데 이보다 더 간단하게

static void dfs(int depth, String result, int idx) {
    if (depth == l) {
        if (check(result)) {
            System.out.println(result);
        }
        return;
    }

    for (int i = idx; i < ch.length; i++) {
        result += Character.toString(ch[i]);
        dfs(depth+1, result, i+1);
        result = result.substring(0,result.length()-1);
    }
}

idx 자리에 i+1을 주어서 자신을 포함하지 않도록 for loop를 돌리면 더 간단하게 수행할 수 있었다.