Algorithm

[SWEA] 4366. 정식이의 은행업무

프로그래민 2020. 5. 2. 14:41
반응형

문자열을 가지고 변환시키면서 모든경우를 다해보는 문제이다.

이 문제는 문자열을 가지고 진법변환을 해주고 HashMap을 사용하는 문제였다. 다만 주의할점은 문자열을 건드는 작업을 할때는 String 클래스를 이용하는 것보다 StringBuilder클래스를 이용해주는 것이 훨씬 시간적으로 이득이라는 점이다. 또한 문자열의 문자를 치환할때 StringBuilder 클래스의 setCharAt()함수를 이용해주면 이문제를 쉽게 풀수있다. 

진법변환에 대해선 10진수를 다른 진수로 바꾸려면 Integer.pasrseInt(Num,N진법수) 와 같은 형태로 바꿀수 있지만 다른진수에서 10진수의 형태로 바꾸려면 직접해주는게 편할 수 있다.

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package swea;
 
 
public class Solution_d4_4366_정식이의은행업무 {
    
    static Map<String, Integer> map;
    public static void main(String[] args) throws Exception {
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int T=Integer.parseInt(br.readLine());
        String temp;
        for(int tc=1;tc<=T;tc++) {
            temp=br.readLine();
            StringBuilder num2=new StringBuilder(temp);
            temp=br.readLine();
            StringBuilder num3=new StringBuilder(temp);
            
 
            map=new HashMap<>();
 
            
            change2(num2);
            change3(num3);
            
            Set<String> keys=map.keySet();
            
            for(String key:keys) {
                if(map.get(key)==2) {
                    System.out.println("#"+tc+" "+key);
                    break;
                }
            }
        }
        
    }
    
    static void change2(StringBuilder num) {
        int size=num.length();
        
        for(int i=0;i<size;i++) {
            if(num.charAt(i)=='0') {
                num.setCharAt(i, '1');
                String input=modTo10(num, 2);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                num.setCharAt(i, '0');
            }else if(num.charAt(i)=='1') {
                num.setCharAt(i, '0');
                String input=modTo10(num, 2);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                num.setCharAt(i, '1');
            }
        }
    }
    static void change3(StringBuilder num) {
        int size=num.length();
        
        for(int i=0;i<size;i++) {
            if(num.charAt(i)=='0') {
                num.setCharAt(i, '1');
                String input=modTo10(num, 3);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                num.setCharAt(i, '2');
                input=modTo10(num, 3);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                
                num.setCharAt(i, '0');
            }else if(num.charAt(i)=='1') {
                num.setCharAt(i, '0');
                String input=modTo10(num, 3);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                num.setCharAt(i, '2');
                input=modTo10(num, 3);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                
                num.setCharAt(i, '1');
            }else if(num.charAt(i)=='2') {
                num.setCharAt(i, '0');
                String input=modTo10(num, 3);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                num.setCharAt(i, '1');
                input=modTo10(num, 3);
                if(map.containsKey(input)) {
                    map.put(input, 2);
                }else
                    map.put(input, 1);
                
                num.setCharAt(i, '2');
            }
        }
    }
    
    
    
    static String modTo10(StringBuilder num,int opt) {
        long result=0;
        if(opt==2) {
            int mul=1;
            for(int index=num.length()-1; index>=0;index--) {
                int n=num.charAt(index)-'0';
                result+=n*mul;
                mul*=2;
            }
            
        }else if(opt==3) {
            int mul=1;
            for(int index=num.length()-1; index>=0;index--) {
                int n=num.charAt(index)-'0';
                result+=n*mul;
                mul*=3;
            }
        }
        
        return String.valueOf(result);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
                                                 
반응형

'Algorithm' 카테고리의 다른 글

[BOJ] 14502. 연구소  (0) 2020.05.02
[BOJ] 15686. 치킨 배달  (0) 2020.05.02
[SWEA] 1949. 등산로 조성  (0) 2020.05.02
[BOJ] 17281.⚾  (0) 2020.05.01
[BOJ] 1339. 단어수학  (0) 2020.04.30