Base64
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1255 Accepted Submission(s): 564
Here is an example of the note in Chinese Passport.
The Ministry of Foreign Affairs of the People's * requests all civil and military authorities of foreign countries to allow the bearer of this passport to pass freely and afford assistance in case of need.
When encoded by \texttt{Base64}, it looks as follows
VGhlIE1pbmlzdHJ5IG9mIEZvcmVpZ24gQWZmYWlycyBvZiB0aGUgUGVvcGxlJ3MgUmVwdWJsaWMgb2Yg
Q2hpbmEgcmVxdWVzdHMgYWxsIGNpdmlsIGFuZCBtaWxpdGFyeSBhdXRob3JpdGllcyBvZiBmb3JlaWdu
IGNvdW50cmllcyB0byBhbGxvdyB0aGUgYmVhcmVyIG9mIHRoaXMgcGFzc3BvcnQgdG8gcGFzcyBmcmVl
bHkgYW5kIGFmZm9yZCBhc3Npc3RhbmNlIGluIGNhc2Ugb2YgbmVlZC4=
In the above text, the encoded result of \texttt{The} is \texttt{VGhl}. Encoded in ASCII, the characters \texttt{T}, \texttt{h}, and \texttt{e} are stored as the bytes 84, 104, and 101, which are the 8-bit binary values 01010100, 01101000, and 01100101. These three values are joined together into a 24-bit string, producing 010101000110100001100101.
Groups of 6 bits (6 bits have a maximum of 26=64 different binary values) are converted into individual numbers from left to right (in this case, there are four numbers in a 24-bit string), which are then converted into their corresponding Base64 encoded characters. The Base64 index table is
0123456789012345678901234567890123456789012345678901234567890123
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
In the above example, the string 010101000110100001100101 is divided into four parts 010101, 000110, 100001 and 100101, and converted into integers 21,6,33and 37. Then we find them in the table, and get V, G, h, l.
When the number of bytes to encode is not divisible by three (that is, if there are only one or two bytes of input for the last 24-bit block), then the following action is performed:
Add extra bytes with value zero so there are three bytes, and perform the conversion to base64. If there was only one significant input byte, only the first two base64 digits are picked (12 bits), and if there were two significant input bytes, the first three base64 digits are picked (18 bits). '=' characters are added to make the last block contain four base64 characters.
As a result, when the last group contains one bytes, the four least significant bits of the final 6-bit block are set to zero; and when the last group contains two bytes, the two least significant bits of the final 6-bit block are set to zero.
For example, base64(A) = QQ==, base64(AA) = QUE=.
Now, Mike want you to help him encode a string for k times. Can you help him?
For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.
In the following T lines, each line contains a case. In each case, there is a number k(1≤k≤5) and a string s. s only contains characters whose ASCII value are from 33 to 126(all visible characters). The length of s is no larger than 100.
1 Mike
4 Mike
Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==
#include <bits/stdc++.h>
using namespace std; char tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int main()
{ //printf("%d %d %d %d\n", 'M', 'i', 'k', 'e');
int t, k;
unsigned char s[];
int i;
int len;
unsigned char a, b, c, d;
unsigned char s2[];
int cas = ;
int j;
int tot;
//char scanf("%d", &t); while (t--) {
scanf("%d%s", &k, s);
len = strlen((const char *)s);
printf("Case #%d: ", ++cas); for (j = ; j < k; ++j) {
tot = ;
for (i = ; i + < len; i += ) {
a = s[i] >> ;
b = s[i] << , b >>= , b += s[i + ] >> ;
c = s[i + ] << , c >>= , c += s[i + ] >> ;
d = s[i + ] << , d >>= ;
//printf("%d %d %d %d\n", a, b, c, d);
//printf("%c%c%c%c", tab[a], tab[b], tab[c], tab[d]);
s2[tot++] = tab[a];
s2[tot++] = tab[b];
s2[tot++] = tab[c];
s2[tot++] = tab[d];
}
if (i == len - ) {
a = s[i] >> ;
b = s[i] << , b >>= ;
//printf("%c%c==\n", tab[a], tab[b]);
s2[tot++] = tab[a];
s2[tot++] = tab[b];
s2[tot++] = '=';
s2[tot++] = '=';
} else if (i == len - ) {
a = s[i] >> ;
b = s[i] << , b >>= , b += s[i + ] >> ;
c = s[i + ] << , c >>= ;
//printf("%c%c%c=\n", tab[a], tab[b], tab[c]);
s2[tot++] = tab[a];
s2[tot++] = tab[b];
s2[tot++] = tab[c];
s2[tot++] = '=';
}
s2[tot] = '\0';
//printf("%s\n", s2);
strcpy((char *)s, (const char *)s2);
len = tot;
//printf("s = %s\n", s);
}
printf("%s\n", s2);
} return ;
}