취미처럼

[백준] 1759번 암호 만들기 본문

Algorithm/백준

[백준] 1759번 암호 만들기

sirius 2021. 3. 3. 09:51
https://www.acmicpc.net/problem/1759
 

1759번: 암호 만들기

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

www.acmicpc.net

 

자음, 모음 수 판별하는 조건 추가

depth가 출력 index 넘지 않도록 조건 추가

 

import java.util.*;
import java.io.*;

public class Main {

public static int L, C;
public static String[] arr;
public static boolean[] visit;
public static String[] ans;
public 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());
    arr = new String[C];
    visit = new boolean[C];
    ans = new String[L];
    st = new StringTokenizer(br.readLine());
    for(int i = 0; i < C; i++) {
    	arr[i] = st.nextToken();
    }

    Arrays.sort(arr);
    dfs(0, 0);
    System.out.println(sb);

    }

    public static void dfs(int start, int depth) {
        if(depth == L && isValid()) {
            for(int j = 0; j < ans.length; j++) {
                sb.append(ans[j]);
            }
        sb.append("\n");
        return;
        }

        for(int i = start; i < C; i++) {
            if(!visit[i] && depth < ans.length) {
                visit[i] = true;
                ans[depth] = arr[i];
                dfs(i + 1, depth + 1);
                visit[i] = false;

            }
        }

    }

    public static boolean isValid() {
        boolean flag = false;
        int mo = 0;
        int ja = 0;
        for(int i = 0 ; i < ans.length; i++) {

            if("a".equals(ans[i]) || "e".equals(ans[i]) || "i".equals(ans[i]) || "o".equals(ans[i]) || "u".equals(ans[i]) ) {
                mo++;
            } else {
                ja++;
            }
        }
        if(mo >= 1 && ja >= 2) {
        	flag = true;
        }
        return flag;

    }
}

'Algorithm > 백준' 카테고리의 다른 글

[백준] 14889번 스타트와 링크  (0) 2021.03.03
[백준] 14501번 퇴사  (0) 2021.03.03
[백준] 18290번 NM과 K (1)  (0) 2021.03.03
[백준] 15657번 N과 M (8)  (0) 2021.03.03
[백준] 15656번 N과 M (7)  (0) 2021.03.03
Comments