티스토리 뷰

반응형

#. Problem

https://www.acmicpc.net/problem/21235

* The copyright in this matter is in acmicpc

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

N 은 최대 100이니까.. 배려심이 좋은 문제다.

 

각 Zodiac 에 순서대로 고유 번호를 부여해서 Map으로 관리하였고,

 

  1         2      3             4          5           6        7          8             9        10    11   12
 Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig, Rat

 

각 소마다 이름, 생년, 띠 를 묶어서 Map 으로 관리하였다.

 

Map 을 잘 사용하면 O(N) 으로 나름.. 무식하지만 생각보다 단순하고 빠르게 풀 수 있다.

 

문자열이 들어오면, 비교대상의 소와 언급된 소의 띠 차이를 계산해서 생년을 계산해준다.

#. 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.io.*;
import java.util.*;
 
public class Main {
 
    FastScanner in;
    PrintWriter out;
 
    static class Cow {
        int year;
        String zodiac;
        
        public Cow(int year, String zodiac) {
            this.year = year;
            this.zodiac = zodiac;
        }
    }
    static Map<String, Integer> zodiacMap =  new HashMap<String, Integer>() {
        {
            put("Ox"1);
            put("Tiger"2);
            put("Rabbit"3);
            put("Dragon"4);
            put("Snake"5);
            put("Horse"6);
            put("Goat"7);
            put("Monkey"8);
            put("Rooster"9);
            put("Dog"10);
            put("Pig"11);
            put("Rat"12);
        }
    };
    void solve() {
        
        int N = in.nextInt();
        Map<String, Cow> birthYears = new HashMap<>();
        birthYears.put("Bessie"new Cow(2021"Ox"));
        for (int i = 0; i < N; i++) {
            String name = in.nextToken();
            in.nextToken(); // born
            in.nextToken(); // in
            String time = in.nextToken(); // previous-next
            String zodiac = in.nextToken();
            in.nextToken(); // year
            in.nextToken(); // from
            String fromName = in.nextToken();
            
            int to = zodiacMap.get(zodiac);
            int from = zodiacMap.get(birthYears.get(fromName).zodiac);
            int y = 0;
            int birth = 0;
            if ("previous".equals(time)) {
                if (to < from)     y = from - to;
                else if (to == from) y = 12;
                else y = from + (12 - to);
                birth = birthYears.get(fromName).year - y;
            } else { // next
                if (to < from) y = (12 - from) + to;
                else if (to == from) y = 12;
                else y = to - from;
                birth = birthYears.get(fromName).year + y;
            }
            
            birthYears.put(name, new Cow(birth, zodiac));
        }    
        
        out.println(Math.abs(birthYears.get("Bessie").year - birthYears.get("Elsie").year));
    }
    
    /********************************** Input function **********************************/
    
    void run() {
        in = new FastScanner();
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }
 
    public static void main(String[] args) {
        new Main().run();
    }
 
    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
 
        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        public FastScanner(String s) {
            try {
                br = new BufferedReader(new FileReader(s));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
 
        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(nextToken());
        }
 
        long nextLong() {
            return Long.parseLong(nextToken());
        }
 
        double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}
cs
반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday