HDU 5237 Base64

Base64

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1435    Accepted Submission(s): 665


Problem Description
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  , and  , which are the  -bit binary values  , and  . These three values are joined together into a 24-bit string, producing  .
Groups of   bits (  bits have a maximum of   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   is divided into four parts   and  , and converted into integers  and  . 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   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  ( ) denoting the number of test cases.
  
  In the following   lines, each line contains a case. In each case, there is a number   and a string   only contains characters whose ASCII value are from   to  (all visible characters). The length of   is no larger than  .
 

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;然后判断原字符串长度,如果len%3==1则补充两个=,若len%3==2,则补充一个=。

#include<bits/stdc++.h>  
#define ll long long  
using namespace std;  
const int maxn = 1e6+5;  
char mod[maxn];  
int str[maxn];  
char ss[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  
void get_bit(int pos, char c) {  
    for(int i = 0; i <= 7; i++) {   //二进制转化  
        str[pos-i] = c%2;  
        c/=2;  
    }  
}  
void get_arr(){  
    int x=0;  
    int len=strlen(mod);  
    memset(str,0,sizeof(str));  
    for(int i = 0; i < len; i++) {  
        get_bit(((i+1)*8-1) ,mod[i]);  
    }  
    fill(mod, mod+maxn, '\0');  
    for(int i = 1; i <= (len+2)/3; i++) {  //将长度默认为3的倍数  
        int pos = (i-1)*24;  
        for(int j=0;j<4;j++){  
            int cnt=0,temp=0;  
            for(int k=pos+(j+1)*6-1;cnt<6;k--){  
                if(str[k]){  
                    temp+=pow(2,cnt);  
                }  
                cnt++;  
            }  
            mod[x++]=ss[temp];  
        }  
    }  
    if(len%3==1){  //判断原字符串的长度,分情况讨论  
        mod[x-1]='=';  
        mod[x-2]='=';  
    } else if(len%3==2){  
    mod[x-1]='=';  
    }  
}  
int main() {  
    int t, T = 0, n;  
    cin>>t;  
    while(t--) {  
       scanf("%d%s",&n,mod);  
        while(n--){  
            get_arr();  
        }     
    cout<<"Case #"<<++T<<": ";  
    cout<<mod<<endl;  
    }  
    return 0;  
}  
自己太菜了,比赛时没 做出来
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值