본문 바로가기

알고리즘

보물상자 비밀번호[swexpert academy]

이슈: 정답을 출력하고 나서, 벡터 arrayList를 초기화를 안해주었다. -_-^

테스트케이스 프린트f로 찍어봐야겠다 앞으론...

 

초기화 초기화 초기화!!!!!!!!!!! 끄아아아아아아아아아아아아아아아아아아아앙 왜 이래 

 

9:07 ~10:30

30분가량 디버깅...

 

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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
 
public class Solution {
    public static int t, n, k;
    public static String input;
    public static int loopCnt;
    public static int std;
    public static long ans;
    public static ArrayList<String> arrList = new ArrayList<String>();
 
    public static void main(String[] args) {
 
        Scanner scan = new Scanner(System.in);
 
        t = scan.nextInt();
        for (int i = 1; i <= t; i++) {
            n = scan.nextInt();
            k = scan.nextInt();
            scan.nextLine();
            input = scan.next();
            std = input.length() / 4;
            input = input + input + input + input + input + input + input + input+input + input + input + input + input + input + input + input;
            loopCnt = decideLoopCnt(n);
            findCode();
            System.out.println("#" + i + " " + ans);
            arrList.clear();
            ans=0;
        }
    }
 
    private static void findCode() {
 
        for (int i = 0; i <= loopCnt; i++) {
            makeArr(i, n + i);
        }
 
        ArrayList<Long> numList = new ArrayList<Long>();
        for (int i = 0; i < arrList.size(); i++) {
            long x = changeNumToDecimal(arrList.get(i));
            numList.add(x);
        }
//        System.out.println(numList.toString());
        Collections.sort(numList);
        int temp = 0;
        for (int i = numList.size() - 1; i >= 0; i--) {
            temp += 1;
            if (temp == k) {
                ans = numList.get(i);
                return;
            }
        }
 
    }
 
    private static long changeNumToDecimal(String str) {
//        System.out.println(str);
        int len = str.length();
        int idx = 0;
        long v = 0;
        for (int i = len - 1; i >= 0; i--) {
            int num = what(str.charAt(i));
//            System.out.println(num);
            long x = pow(idx++);
//            System.out.println("x:"+x);
            v = v + num * x;
        }
        return v;
    }
 
    private static long pow(int idx) {
        long num = 1;
        for (int i = 1; i <= idx; i++) {
            num = num * 16;
        }
        return num;
    }
 
    private static int what(char ch) {
        if (ch >= 48 && ch <= 57)
            return (int) ch - 48;
        if (ch >= 65)
            return (int) ch - 55;
 
        return 0;
    }
 
    private static void makeArr(int s, int e) {
 
        StringBuilder sb = new StringBuilder();
        int cnt = 0;
        for (int i = s; i < e; i++) {
            sb.append(input.charAt(i));
            cnt += 1;
            if (cnt == std) {
                if (!arrList.contains(sb.toString())) {
                    arrList.add(sb.toString());
                }
                cnt = 0;
                sb = new StringBuilder();
            }
        }
 
    }
 
    private static int decideLoopCnt(int x) {
        if (x == 8)
            return 2;
        if (x == 12)
            return 3;
        if (x == 16)
            return 4;
        if (x == 20)
            return 5;
        if (x == 24)
            return 6;
        if (x == 28)
            return 7;
        return 0;
    }
}
 
cs

 

'알고리즘' 카테고리의 다른 글

핀볼 게임 [swexpert academy]  (0) 2019.10.07
벽돌 깨기[swexpert academy]  (0) 2019.10.02
활주로건설[swexpert academy]  (0) 2019.10.01
홈방범서비스[swexpertacademy]  (0) 2019.10.01
디저트카페[swexpert academy]  (0) 2019.10.01