티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in BOJ




#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve


기본적인 조합문제이다.


조교들은 정렬된 문자열을 선호하므로 주어진 알파벳을 정렬해주고

조합을 돌려주면 된다.


살짝(?) 신경써주어야 할 부분은 

암호가 최소 한 개의 모음과 최소 두 개의 자음으로 구성된다는 점!


#. Code


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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class BOJ1759 {
 
    static int L, C;
    static char alpa[], sel[];
    static StringBuilder sb;
    
    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        sb = new StringBuilder();
        
        L = Integer.parseInt(st.nextToken()); // 암호 자리수
        C = Integer.parseInt(st.nextToken()); // 사용했을 법한 문자의 종류
        
        alpa = new char[C];
        sel = new char[L];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < C; i++) {
            alpa[i] = st.nextToken().charAt(0);
        }
        // 조교들은 정렬된 문자열을 선호
        Arrays.sort(alpa);
 
        process(0000);
        
        System.out.println(sb);
    }
 
    // 최소 한 개의 모음과 최소 두 개의 자음으로 구성
    private static void process(int idx, int cnt, int v, int c) {
        // 조합이 완성되었을 경우
        if(cnt == L) {
            if(v >= 1 && c >= 2) {
                for (int i = 0; i < L; i++) {
                    sb.append(sel[i]);
                }
                sb.append('\n');
            }
            
            return;
        }
        
        for (int i = idx; i < C; i++) {
            sel[cnt] = alpa[i];
            
            if(alpa[i] == 'a' || alpa[i] == 'e' || alpa[i] == 'i' || alpa[i] == 'o' || alpa[i] == 'u')
                process(i + 1, cnt + 1, v + 1, c);
            else
                process(i + 1, cnt + 1, v , c + 1);
        }
    }
 
}
 
cs





반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday