题目:https://cn.vjudge.net/contest/160646#problem/B
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 84 ,
104, and
101, which are the 8 8-bit binary values 01010100 01010100, 01101000 01101000, and 01100101 01100101. These three values are joined together into a 24-bit string, producing 010101000110100001100101 010101000110100001100101.
Groups of 6 6 bits ( 6 6 bits have a maximum of 26=64 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 010101000110100001100101 is divided into four parts 010101 010101, 000110 000110, 100001 100001 and 100101 100101, and converted into integers 21,6,33 21,6,33 and 37 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 k times. Can you help him?
For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.
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 84 ,
104, and
101, which are the 8 8-bit binary values 01010100 01010100, 01101000 01101000, and 01100101 01100101. These three values are joined together into a 24-bit string, producing 010101000110100001100101 010101000110100001100101.
Groups of 6 6 bits ( 6 6 bits have a maximum of 26=64 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 010101000110100001100101 is divided into four parts 010101 010101, 000110 000110, 100001 100001 and 100101 100101, and converted into integers 21,6,33 21,6,33 and 37 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 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 T lines, each line contains a case. In each case, there is a number k(1≤k≤5) k(1≤k≤5) and a string s s. s s only contains characters whose ASCII value are from 33 33 to 126 126(all visible characters). The length of s s is no larger than 100 100.
2 1 Mike 4 Mike
Case #1: TWlrZQ== Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==
题意:将每个字符(的ASCII码)化为8位的2进制,3个字符一组组成连续的24个二进制,然后将其划分为4段(6个一段),再将每段转化为对应的64进制的字符
NOTE:做题时没有空格的输入,当最后不够三个字符时,若剩1个,将此字符的对应的8位2进制放在24个2进制对应的前端(也就是只能划分出2段6个2进制),后两个64进制的字符为‘=’;若剩2个,与一个相同(只是只可以划分出3段6个2进制)最后一个字符为‘=’,k的意思为将输入字符转化k次,也就是对于每次转化得到的字符串再进行换转换
思路:
此题虽然A了,但是做的很慢,题意有些复杂,有些麻烦的题目,但看大家还是都有做出来,其实此题最终是将3个字符转化为4个字符
代码:
#include<bits/stdc++.h>
using namespace std;
int two(int n,int *x)
{
int i=8;
do
{
x[i]=n%2;
i--;
n=n/2;
}while(n!=0);
}
char base[65];
int main()
{
//freopen("aaa.txt","r",stdin);
int t,k,i,j,len,flag,ca=0;
cin>>t;
string s;
int a[9],b[9],c[9];
int d[29];
for(i=0;i<26;i++)
base[i]='A'+i;
for(i=26;i<52;i++)
base[i]='a'+i-26;
for(i=52;i<62;i++)
base[i]='0'+i-52;
base[62]='+'; base [63]='/';
while(t--)
{
ca++;
scanf("%d",&k);
cin>>s;
while(k--) {
len=s.size();
string ss;
for(i=3;i<=len;i+=3)
{
//if(i+3>len) break;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
two(s[i-3],a);
two(s[i-2],b);
two(s[i-1],c);
for(j=1;j<=8;j++)
{
d[j]=a[j];
d[j+8]=b[j];
d[j+16]=c[j];
}
int v1,v2,v3,v4;
v1=v2=v3=v4=0;
for(j=1;j<=6;j++)
{
v1=v1*2+d[j];
v2=v2*2+d[j+6];
v3=v3*2+d[j+12];
v4=v4*2+d[j+18];
}
//cout<<v1<<" "<<v2<<" "<<v3<<" "<<v4<<endl;
//cout<<base[v1]<<base[v2]<<base[v3]<<base[v4]<<endl;
ss+=base[v1]; ss+=base[v2]; ss+=base[v3]; ss+=base[v4];
}
if(len%3==1) {
memset(a,0,sizeof(a));
memset(d,0,sizeof(d));
two(s[len-1],a);
for(j=1;j<=8;j++)
{
d[j]=a[j];
}
int v1,v2;
v1=v2=0;
for(j=1;j<=6;j++)
{
v1=v1*2+d[j];
v2=v2*2+d[j+6];
}
//cout<<v1<<" "<<v2<<" "<<v3<<" "<<v4<<endl;
//cout<<base[v1]<<base[v2]<<"=="<<endl;
ss+=base[v1]; ss+=base[v2]; ss+='='; ss+='=';
}
else if(len%3==2) {
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(d,0,sizeof(d));
two(s[len-2],a);
two(s[len-1],b);
for(j=1;j<=8;j++)
{
d[j]=a[j];
d[j+8]=b[j];
}
int v1,v2,v3;
v1=v2=v3=0;
for(j=1;j<=6;j++)
{
v1=v1*2+d[j];
v2=v2*2+d[j+6];
v3=v3*2+d[j+12];
}
//cout<<v1<<" "<<v2<<" "<<v3<<" "<<v4<<endl;
//cout<<base[v1]<<base[v2]<<base[v3]<<"="<<endl;
ss+=base[v1]; ss+=base[v2]; ss+=base[v3]; ss+='=';
}
s=ss;
}
cout<<"Case #"<<ca<<":"<<" ";
cout<<s<<endl;
}
return 0;
}