hdu 5237 2015上海邀请赛 B - Base64 (进制转化,模拟)

int n;
string s;
map<int,char>mp;
void solve1()
{
    char A='A';
    for(int i=0;i<26;i++)mp[i]=(char)(A+i);
    A='a';
    for(int i=0;i<26;i++)mp[i+26]=(char)(A+i);
    A='0';
    for(int i=0;i<10;i++)mp[i+52]=(char)(A+i);
    mp[62]='+';mp[63]='/';
    //for(int i=0;i<64;i++)cout<<mp[i];
}
void solve2()
{
    string str="",tmp;
    for(int i=0;i<s.length();i++)
    {
        tmp="";
        int x=int(s[i]);
        for(int i=0;i<8;i++)
        {
            tmp+=char('0'+x%2);
            x>>=1;
        }
        for(int i=7;i>=0;i--)
            str+=tmp[i];
    }
    //cout<<str<<endl;
    s=str;
}
void solve3()
{
    string str="";
    int len=s.length();
    //cout<<s<<' '<<len<<endl;
    if(len%6)
    {
        int t=6-len%6;
        for(int i=0;i<t;i++)
            s+='0';
    }
    for(int i=0;i<s.length();i+=6)
    {
        int t=0;
        t+=int(s[i]-'0')*32;
        t+=int(s[i+1]-'0')*16;
        t+=int(s[i+2]-'0')*8;
        t+=int(s[i+3]-'0')*4;
        t+=int(s[i+4]-'0')*2;
        t+=int(s[i+5]-'0');
        str+=mp[t];
    }
    //cout<<str<<endl;
    s=str;
}
void solve4()
{
    int len=s.length();
    if(len%4)
    {
        int t=4-len%4;
        for(int i=0;i<t;i++)
            s+='=';
    }
}
int main()
{
    ios::sync_with_stdio(false);
    solve1();
    int T;
    cin>>T;
    int cas=1;
    while(T--)
    {
        cin>>n>>s;
        for(int i=0;i<n;i++)
        {
            solve2();
            solve3();
            solve4();
        }
        cout<<"Case #"<<cas++<<": "<<s<<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 8484104104, and 101101, which are the 88-bit binary values 01010100010101000110100001101000, 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 010101010101000110000110100001100001 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(T20T≤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(1k5)k(1≤k≤5) and a string ssss 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==

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值