16진수 수가 최대 28개 주어지고 이를 4개의 동일한 길이를 가진 구간으로 나눴을 때 나타나는 숫자들 중 K번째 숫자를 출력하는 문제다.


숫자로 바꿔도 4*7 = 28bit라서 int에 충분히 들어간다.


set이나 map을 써도 되고 vector에 넣은 다음 정렬하고 앞에서부터 유일한것 개수를 세고 들어가도 시간이 남아돈다.


erase(unique())해도 된다.


또한 아스키 코드상 A,B,C,D,E,F는 1~9보다 높은 값을 가지고 있으므로 int로 안바꾸고 문자열 그대로 사용한 후 결과만 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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
 
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
 
    int t, tc, N, K;
    int sco[128];
    string S;
 
    cin >> t;
    for (char i = '0'; i <= '9'++i) sco[i] = i - '0';
    for (char i = 'A'; i <= 'F'++i) sco[i] = 10 + i - 'A';
 
    for (tc = 1; tc <= t; ++tc) {
        vector<string> arr;
        cin >> N >> K >> S;
        S += S;
        for (int i = 0; i < N; ++i) 
            arr.push_back(S.substr(i,N >> 2));
        sort(arr.begin(), arr.end());
        arr.erase(unique(arr.begin(), arr.end()), arr.end());
        int ans = 0;
        for (int i = 0; i < (N >> 2); ++i)
            ans = (ans << 4+ sco[arr[arr.size() - K][i]];
        cout << "#" << tc << " " << ans << '\n';
    }
    return 0;
}
cs


'SWEA' 카테고리의 다른 글

5644. 무선 충전  (0) 2018.09.27
5648. 원자 소멸 시뮬레이션  (0) 2018.09.21
5656. 벽돌 깨기  (0) 2018.09.21
5653. 줄기세포 배양  (4) 2018.09.21
5216. 다항식의 계수  (0) 2018.09.04

+ Recent posts