您的位置:首页 > 编程语言 > Java开发

SDKD 2017 Spring Team Training A--B - Base64

2017-04-29 23:32 330 查看
题目: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 bytes84, 
104,
and 
101,
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,33 and 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==


题意:将每个字符(的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: