티스토리 뷰

반응형


#. 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


뭔가 조건이 복잡해보이지만..!

중요한 포인트는

1. wolf 순서대로 단어가 이루어져 있는지

2. 모든 문자가 같은 횟수로 반복되는지

이다.


중복을 제거한 'wolf'를 담을 배열과

각 문자의 반복 횟수를 담은 배열을 사용하였다.


4글자가 완성되었을 때,

올바른 단어인지 체크를 해주었다.

wolf 순서대로 이루어져있고, 모든 문자가 같은 횟수로 반복되어있는지..!


#. 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
62
63
64
65
66
67
68
69
70
71
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class BOJ13022 {
 
    static char[] wolf = { 'w''o''l''f' };
 
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        String input = br.readLine();
 
        System.out.println(process(input));
    }
 
    private static int process(String str) {
 
        char[] arr = new char[4];
        int[] cnt = new int[26];
        // 첫 문자는 먼저 넣어두자.
        char prev = str.charAt(0);
        arr[0= str.charAt(0);
        cnt[str.charAt(0- 'a']++;
        int idx = 1;
        
        for (int i = 1; i < str.length(); i++) {
            char now = str.charAt(i);
            // 이전 문자와 같다면 pass
            if(now == prev) {
                cnt[now - 'a']++;
                continue;
            }
            
            // 다르다면
            // 'w'로 다시 시작할 경우.
            if(idx == 4) {
                // 순서 및 반복횟수 확인
                if(!check(arr, cnt)) return 0;
                
                // cnt 배열과 index 초기화
                cnt = new int[26];
                idx = 0;
            }
            
            arr[idx++= now;
            cnt[now - 'a']++;
            prev = now;
        }
 
        // 길이가 4인 경우도 확인 필요
        if(!check(arr, cnt)) return 0;
        
        return 1;
    }
 
    private static boolean check(char[] arr, int[] cnt) {
        
        int flag = cnt[arr[0- 'a'];
        for (int ck = 0; ck < 4; ck++) {
            // 순서 확인
            if(arr[ck] != wolf[ck]) return false;
            // 반복횟수 확인
            if(cnt[arr[ck] - 'a'!= flag) return false;
        }
        
        return true;
    }
 
}
cs


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