Base64 (HDU - 5237)
2017-04-30 18:09
246 查看
点击打开原题链接
Mike does not want others to view his messages, so he find a encode method Base64.
Here is an example of the note in Chinese Passport.
The Ministry of Foreign Affairs of the People's Republic of China 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 8484, 104104,
and 101101,
which are the 88-bit
binary values 0101010001010100, 0110100001101000,
and 0110010101100101.
These three values are joined together into a 24-bit string, producing 010101000110100001100101010101000110100001100101.
Groups of 66 bits
(66 bits
have a maximum of 26=6426=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 010101000110100001100101010101000110100001100101 is
divided into four parts 010101010101, 000110000110, 100001100001 and 100101100101,
and converted into integers 21,6,3321,6,33and 3737.
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 kk times.
Can you help him?
For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.
Input The first line contains an integer TT(T≤20T≤20)
denoting the number of test cases.
In the following TT lines,
each line contains a case. In each case, there is a number k(1≤k≤5)k(1≤k≤5) and
a string ss. ss only
contains characters whose ASCII value are from 3333 to 126126(all
visible characters). The length of ss is
no larger than 100100.
Output For each test case, output Case #t:, to represent this is t-th case. And then output the encoded string.
Sample Input
Sample Output
题意:算是很水的一个题,题目很长,吓人一跳,大体意思就是给你一个字符串,写出每个字符的二进制(不组八位的前面补0),然后依次把每个字符的二进制01串加起来,然后如果原字符串的长度%3==1的话,那么01串后面补四个零;如果原字符串的长度%3==2,那么01串后边补上2个0。最后解码。
详见代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
char code[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string op[100]={"00100001","00100010","00100011","00100100","00100101","00100110","00100111","00101000","00101001","00101010","00101011","00101100","00101101","00101110","00101111","00110000","00110001","00110010","00110011","00110100","00110101","00110110","00110111","00111000","00111001","00111010","00111011","00111100","00111101","00111110","00111111","01000000","01000001","01000010","01000011","01000100","01000101","01000110","01000111","01001000","01001001","01001010","01001011","01001100","01001101","01001110","01001111","01010000","01010001","01010010","01010011","01010100","01010101","01010110","01010111","01011000","01011001","01011010","01011011","01011100","01011101","01011110","01011111","01100000","01100001","01100010","01100011","01100100","01100101","01100110","01100111","01101000","01101001","01101010","01101011","01101100","01101101","01101110","01101111","01110000","01110001","01110010","01110011","01110100","01110101","01110110","01110111","01111000","01111001","01111010","01111011","01111100","01111101","01111110"};
string in;
int main()
{
/*char cc[200];
for(int i=33;i<=126;i++)
{
char pp[100];
int sum = 0;
int t=i;
while(t)
{
char c;
if(t%2==0) c = '0';
else c = '1';
pp[sum] = c;
t /= 2;
sum++;
}
while(sum<8)
{
pp[sum] = '0';
sum++;
}
reverse(pp,pp+8);
pp[8] = '\0';
printf("\"");
printf("%s",pp);
printf("\"");
printf(",");
}*/
// 以上为打表专用 ,提前求出八位的二进制,存在op数组中。op[0]存储的是ascii表中 值为33的字符,往后依次递加。
int T;
scanf("%d",&T);
for(int rr=1;rr<=T;rr++)
{
int k;
scanf("%d",&k);
string sum;
string bu;
cin >> in;
//cout << in << endl;
string fuin;
fuin="";
sum = "";
for(int w=0;w<k;w++)
{
int len = in.size();
//cout << len << endl;
if(len%3==1)
{
bu = "==";
for(int i=0;i<len;i++)
{
sum += op[(int)in[i]-33];
}
for(int i=1;i<=4;i++)
{
sum += "0";
}
}
else if(len%3==2)
{
bu = "=";
for(int i=0;i<len;i++)
{
sum += op[(int)in[i]-33];
}
for(int i=1;i<=2;i++)
{
sum += "0";
}
}
else if(len%3==0)
{
bu = "";
for(int i=0;i<len;i++)
{
sum += op[(int)in[i]-33];
}
}
int p = 0;
int ttt = sum.size();
while(p<ttt)
{
int num = 0;
int er = 1;
for(int i=p+5;i>=p;i--)
{
int e = sum[i] - '0';
num += er*e;
er *= 2;
}
//cout << num << endl;
fuin += code[num];
p += 6;
}
in = fuin+bu;
fuin = "";
sum = "";
//cout << in << endl;
}
printf("Case #%d: ",rr);
cout << in << endl;
}
return 0;
}
水波。
Mike does not want others to view his messages, so he find a encode method Base64.
Here is an example of the note in Chinese Passport.
The Ministry of Foreign Affairs of the People's Republic of China 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 8484, 104104,
and 101101,
which are the 88-bit
binary values 0101010001010100, 0110100001101000,
and 0110010101100101.
These three values are joined together into a 24-bit string, producing 010101000110100001100101010101000110100001100101.
Groups of 66 bits
(66 bits
have a maximum of 26=6426=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 010101000110100001100101010101000110100001100101 is
divided into four parts 010101010101, 000110000110, 100001100001 and 100101100101,
and converted into integers 21,6,3321,6,33and 3737.
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 kk times.
Can you help him?
For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.
Input The first line contains an integer TT(T≤20T≤20)
denoting the number of test cases.
In the following TT lines,
each line contains a case. In each case, there is a number k(1≤k≤5)k(1≤k≤5) and
a string ss. ss only
contains characters whose ASCII value are from 3333 to 126126(all
visible characters). The length of ss is
no larger than 100100.
Output For each test case, output Case #t:, to represent this is t-th case. And then output the encoded string.
Sample Input
2 1 Mike 4 Mike
Sample Output
Case #1: TWlrZQ== Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==
题意:算是很水的一个题,题目很长,吓人一跳,大体意思就是给你一个字符串,写出每个字符的二进制(不组八位的前面补0),然后依次把每个字符的二进制01串加起来,然后如果原字符串的长度%3==1的话,那么01串后面补四个零;如果原字符串的长度%3==2,那么01串后边补上2个0。最后解码。
详见代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
char code[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string op[100]={"00100001","00100010","00100011","00100100","00100101","00100110","00100111","00101000","00101001","00101010","00101011","00101100","00101101","00101110","00101111","00110000","00110001","00110010","00110011","00110100","00110101","00110110","00110111","00111000","00111001","00111010","00111011","00111100","00111101","00111110","00111111","01000000","01000001","01000010","01000011","01000100","01000101","01000110","01000111","01001000","01001001","01001010","01001011","01001100","01001101","01001110","01001111","01010000","01010001","01010010","01010011","01010100","01010101","01010110","01010111","01011000","01011001","01011010","01011011","01011100","01011101","01011110","01011111","01100000","01100001","01100010","01100011","01100100","01100101","01100110","01100111","01101000","01101001","01101010","01101011","01101100","01101101","01101110","01101111","01110000","01110001","01110010","01110011","01110100","01110101","01110110","01110111","01111000","01111001","01111010","01111011","01111100","01111101","01111110"};
string in;
int main()
{
/*char cc[200];
for(int i=33;i<=126;i++)
{
char pp[100];
int sum = 0;
int t=i;
while(t)
{
char c;
if(t%2==0) c = '0';
else c = '1';
pp[sum] = c;
t /= 2;
sum++;
}
while(sum<8)
{
pp[sum] = '0';
sum++;
}
reverse(pp,pp+8);
pp[8] = '\0';
printf("\"");
printf("%s",pp);
printf("\"");
printf(",");
}*/
// 以上为打表专用 ,提前求出八位的二进制,存在op数组中。op[0]存储的是ascii表中 值为33的字符,往后依次递加。
int T;
scanf("%d",&T);
for(int rr=1;rr<=T;rr++)
{
int k;
scanf("%d",&k);
string sum;
string bu;
cin >> in;
//cout << in << endl;
string fuin;
fuin="";
sum = "";
for(int w=0;w<k;w++)
{
int len = in.size();
//cout << len << endl;
if(len%3==1)
{
bu = "==";
for(int i=0;i<len;i++)
{
sum += op[(int)in[i]-33];
}
for(int i=1;i<=4;i++)
{
sum += "0";
}
}
else if(len%3==2)
{
bu = "=";
for(int i=0;i<len;i++)
{
sum += op[(int)in[i]-33];
}
for(int i=1;i<=2;i++)
{
sum += "0";
}
}
else if(len%3==0)
{
bu = "";
for(int i=0;i<len;i++)
{
sum += op[(int)in[i]-33];
}
}
int p = 0;
int ttt = sum.size();
while(p<ttt)
{
int num = 0;
int er = 1;
for(int i=p+5;i>=p;i--)
{
int e = sum[i] - '0';
num += er*e;
er *= 2;
}
//cout << num << endl;
fuin += code[num];
p += 6;
}
in = fuin+bu;
fuin = "";
sum = "";
//cout << in << endl;
}
printf("Case #%d: ",rr);
cout << in << endl;
}
return 0;
}
水波。
相关文章推荐
- HDU 5237 Base64 (Java大法好)
- HDU 5237 Base64
- HDU - 5237 Base64(字符串)
- HDU 5237 Base64
- HDU 5237 Base64 模拟
- hdu 5237 Base64(模拟)
- HDU - 5237 Base64 2015上海邀请赛 (模拟)
- HDU 5237 Base64
- hdu-5237 Base64-The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest
- HDU 5237 Base64
- HDU 5237-Base64(模拟-K轮加密)
- HDU 5237 Base64
- hdu 5237 Base64
- hdu 5237 2015上海邀请赛 B - Base64 (进制转化,模拟)
- HDU 5237 Base64
- hdu 5237__Base64
- HDOJ 5237 Base64
- HDU-5237 模拟
- HDU1008——Elevator
- HDU 分类(updating)