swea::5658 보물상자 비밀번호

시사점

이해(3)

설계, 손 코딩(3)

복잡도

구현(15)

#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int n, th;
int hex2dec(string s){
    int ret = 0, itr = 1;
    reverse(all(s));
    rep(i, 0, s.size()){
        if(s[i] >= 'A'){
            ret += (s[i] - 'A' + 10) * itr;
        }else{
            ret += (s[i] - '0') * itr;
        }
        itr *= 16;
    }
    return ret;
}
void process(){
    cin >> n >> th;
    string s; cin >> s;
    vector<string> ans;
    // shifts
    rep(i, 0, (n/4)){
        if(i) s =  s[n-1] + s.substr(0, n-1);
        // cut
        for(int j = 0; j < n; j += (n/4)){
            ans.pb({s.substr(j, n/4)});
        }
    }
    sort(all(ans));
    ans.erase(unique(all(ans)), ans.end());
    reverse(all(ans));
    cout << hex2dec(ans[th-1]) << endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int tc; cin >> tc;
    rep(t, 1, tc+1){
        cout << "#" << t <<" ";
        process();
    }
    return 0;
}

디버깅(x)

좋은 코드

최적화