전공공부/코딩테스트
(c++) SWEA "1928) Base64 Decoder"
by 시아나
2022. 5. 28.
https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=1&problemLevel=2&problemLevel=3&contestProbId=AV5PR4DKAG0DFAUq&categoryId=AV5PR4DKAG0DFAUq&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=CCPP&select-1=3&pageSize=10&pageIndex=2
#include <iostream>
#include <cctype>
#include <string>
#include <bitset>
#include <cmath>
using namespace std;
int decode(char c) {
if (isupper(c)) {
return (c - 'A');
}
else if (islower(c)) {
return (c - 'a' + 26);
}
else if (isdigit(c)) {
return c - '0' + 52;
}
else {
if (c == '+') {
return 62;
}
else {
return 63;
}
}
}
string solution(string str) {
string answer;
string nums;
for (int i = 0; i < str.size();i++) {
if (i > 0 && i % 4 == 0) {
for (int j = 0; j < 3; j++) {
string tmp = nums.substr(j * 8, 8);
char c = 0;
for (int k = 0; k < tmp.size(); k++) {
c += pow(2, 8 - k - 1) * (tmp[k] - '0');
}
answer.push_back(c);
}
nums = "";
}
int num = decode(str[i]);
nums += bitset<6>(num).to_string();
}
//tmp가 남아있을 경우 확인
for (int j = 0; j < nums.size()/8; j++) {
string tmp = nums.substr(j * 8, 8);
char c = 0;
for (int k = 0; k < tmp.size(); k++) {
c += pow(2, 8 - k - 1) * (tmp[k] - '0');
}
answer.push_back(c);
}
return answer;
}
int main() {
int T; cin >> T;
for (int t = 1; t <= T; t++) {
string str; cin >> str;
cout << "#" << t << " " << solution(str) << endl;
}
return 0;
}