Algorithm 문제풀이

[BOJ] 백준 1759 - 암호만들기 (JAVA)

cjsong 2019. 4. 28. 21:25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
public class Main1759_암호만들기 {
    private static int C;
    private static int L;
    private static char[] alp;
    private static ArrayList<Character> list;
    private static int Z;
    private static int M;
    private static StringBuilder sb;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        sb = new StringBuilder();
        L = Integer.parseInt(st.nextToken()); // nCr -> r 의미
        C = Integer.parseInt(st.nextToken()); // n 의미
        list = new ArrayList<Character>();
        st = new StringTokenizer(br.readLine(), " ");
        alp = new char[C];
        Z = 0// 모음 갯수
        M = 0// 자음 갯수
        for (int i = 0; i < C; i++) {
            alp[i] = st.nextToken().charAt(0);
        }
        Arrays.sort(alp); // 정렬된 문자열을 선호한다고 했으니깐 sort해줌.
        comb(00);
        System.out.println(sb);
    }// end of main
 
    public static void comb(int idx, int k) {
        if (idx == L) {
            Z = 0;
            M = 0;
            for (int i = 0; i < L; i++) { // 자음갯수 모음갯수 파악
                if (list.get(i) == 'a' || list.get(i) == 'e' || list.get(i) == 'i' || list.get(i) == 'o'
                        || list.get(i) == 'u') {
                    Z++;
                } else {
                    M++;
                }
            }
 
            if (Z >= 1 && M >= 2) { // 문제에서 모음의 갯수가 최소 1개 모음의 갯수가 2개일때
                for (int i = 0; i < list.size(); i++) {
                    sb.append(list.get(i));
                }
                sb.append("\n");
                return;
            } else {
                return;
            }
 
        }
 
        for (int i = k; i < alp.length; i++) { // 조합
            list.add(alp[i]);
            comb(idx + 1, i + 1);
            list.remove(list.size() - 1);
        }
    }
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs