HDU5237——模拟——Base64

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  84104, and 101, which are the 8-bit binary values 0101010001101000, and 01100101. These three values are joined together into a 24-bit string, producing 010101000110100001100101.
Groups of 6 bits (6 bits have a maximum of 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 is divided into four parts 010101000110100001 and 100101, and converted into integers 21,6,33 and 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 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  T(T20) denoting the number of test cases.
  
  In the following T lines, each line contains a case. In each case, there is a number k(1k5) and a string ss only contains characters whose ASCII value are from 33 to 126(all visible characters). The length of s is no larger than 100.
 

 

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==
 

 

Source
 

 

Recommend
We have carefully selected several similar problems for you:   5245  5244  5243  5242  5241 
大意:第二个样例卡了1天~~~先考虑被3整除的,在考虑余数,被3整除的块,先转化成24个二进制数,分成4块,转化成6进制数,对于s2,进行字符化,余数,为1的现在有8个字节,为了使得被6整除,后面加4个零,现在有两个字符,为了保证4个字符,后面再加两个‘=’,如果余数为2的话有16个字节,为了使得被6整除,后面加上2个零,变成18,现在有三个字符,后面加上一个‘=’,注意读入用gets,并且不能吃进第一个‘ ’,还有坑点是s转化之后是三个变四个,如果用同一个的话会把第四个给更新掉,就不是原来的字符串了,先保存到另一个字符串里面,再strcpy进去
渣渣代码
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

int T,k;

char s[1100];

char s3[1100];

char s2[50];

int a[110];

char s1[100] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int n,x,y;

int flag,num;

void base()   

{ 

    flag = 1;

    memset(s2,0,sizeof(s2));

    n = strlen(s+1);

    //puts(s);

   // printf("%d\n",n);

         x = n%3;

         y = n/3;

        s2[1] = s[3*y+1],s2[2] = s[3*y+2];

        // printf("%d %d \n",x,y);

        for(int i = 1 ; i <= 3*y; i++){

             num = s[i] ; 

            if(i%3 == 1){

            for(int i = 8; i >= 1; i--){

                a[i] = num%2;

                num/= 2;

            }

         }

            else if(i%3 == 2){

                for(int i = 16; i >= 9; i--){

                a[i] = num%2;

                num/= 2;

                }

            }

            else if(i%3 == 0){

                for(int i = 24; i >= 17; i--){

                a[i] = num%2;

                num/= 2;

                }

               // for(int i = 1; i <= 24; i++)

               //     printf("%d",a[i]);

                num = 0;

                for(int i = 1; i <= 24; i++){

                    if(i%6 == 0){

                        num = num * 2 + a[i];

                        s3[flag++] = s1[num];

                        num = 0;

                    }

                    else {

                        num = num * 2 + a[i];

                    }

                }

            }

        }

       // printf("%d",flag);

       // puts(s+1);

        if(x == 1){

            for(int i = 12; i >= 9; i--)

                a[i] = 0;

            num = s2[1];

           // printf("%d\n",num);

            for(int i = 8; i >= 1; i--){

                a[i] = num%2;

                num/= 2;

            } 

         //   for(int i = 1; i <= 8; i++)

         //      printf("%d",a[i]);

        num = 0;

        for(int i = 1; i <= 12; i++){

            if(i%6 == 0 ){

                num = num*2 + a[i];

               // printf("%d\n",num);

                s3[flag++] = s1[num];

               // printf("%c",s[1]);

                num = 0;

            }

            else 

                num = num*2 + a[i];

        }

        s3[flag++] = '=';

        s3[flag++] = '=';

    }

        else if(x == 2){

            for(int i = 18; i >= 17; i--)

                a[i] = 0;

            num = s2[2];

            for(int i = 16; i >= 9; i--){

                a[i] = num%2;

                num/= 2;

            }

            num = s2[1] ;

            // printf("%c\n",s2[1]);

            //printf("%c\n",s2[2]);

            for(int i = 8; i >= 1; i--){

                a[i] = num%2;

                num/=2;

            }

            num = 0;

            for(int i = 1; i <= 18; i++){

                if(i%6 == 0 ){

                 num = num*2 + a[i];

                 s3[flag++] = s1[num];

                 num = 0;

                }

                else 

                    num = num * 2 + a[i];

                }

            s3[flag++] = '=';

            }

      //  puts(s3+1);

        strcpy(s+1,s3+1);

      //  puts(s+1);

        memset(s3,0,sizeof(s3));

}

int main()

{

    int n,num;

    scanf("%d",&T);

    for(int cas = 1; cas <= T; cas++){

        memset(s,0,sizeof(s));

        memset(s2,0,sizeof(s2));

        scanf("%d",&k);

        getchar();

        gets(s+1);

        //puts(s+1);

        for(int i = 1; i <= k;i++){

         //   puts(s+1);

            base();

        }

        printf("Case #%d: ",cas);

        for(int i = 1; i < flag ; i++)

            printf("%c",s[i]);

        puts("");

    }

    return 0;

}

  

 

你可能感兴趣的:(base64)