1 minute read

문제 풀이 방식

  • 전반적으로 15655 와 유사하며, 구현 조건에 유의하자.

문제 풀이 (Java)

import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.util.Arrays;  
import java.util.StringTokenizer;  
  
public class boj1759 {  
    static int L, C;  
    static char[] foo;  
  
    static char[] tmp;  
    static boolean[] visit;  
  
    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());  
  
        foo = new char[C];  
        tmp = new char[L];  
        visit = new boolean[C];  
  
        st = new StringTokenizer(br.readLine());  
        for(int i = 0; i < C; i++){  
            foo[i] = st.nextToken().charAt(0);  
        }  
  
        Arrays.sort(foo);  
  
        DFS(0, 0);  
        System.out.print(sb);  
    }  
  
    public static void DFS(int idx, int depth){  
        if (depth == L){  
            // 탐색 종료 조건 : 길이가 L
            if (check(tmp)) printArray(tmp);  
            return;  
        }  
  
        // 재귀 호출을 통한 DFS 구현  
        for(int i = idx; i < C; i++){  
            if(!visit[i]){  
                visit[i] = true;  
                tmp[depth] = foo[i];  
                DFS( i, depth+1);  
                visit[i] = false;  
            }  
  
  
        }  
  
    }  
    public static boolean check(char[] arr){  
        boolean flag = false;  
        int cnt = 0;  
        for(int i = 0; i < arr.length ; i++){  
            if (arr[i] == 'a' || arr[i] == 'e'  
                    || arr[i] == 'i' || arr[i] == 'o'  
                    || arr[i] == 'u') flag = true;  
            else cnt++;  
        }  
        return (cnt >= 2) && flag;  
    }  
  
    public static void printArray(char[] arr){  
  
        for(int i = 0; i < arr.length ; i++){  
            sb.append(arr[i]);  
        }  
        sb.append('\n');  
    }  
}

Leave a comment