码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5237 Base64

时间:2015-06-04 15:46:11      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

Base64

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


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 bytes84技术分享,104技术分享, and 101技术分享, which are the 8技术分享-bit binary values 01010100技术分享,01101000技术分享, 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 2技术分享6技术分享=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 010101技术分享,000110技术分享,100001技术分享 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 s技术分享.s技术分享 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==

题意:将3个8位变成4个6位,不足八位的时候补0,最后不足四位补=,求出k次变换后的结果

#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
char e[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


//int main()
//{
//    int k=11;
//    for(int i=7;i>=0;i--)
//    cout<<(k>>i&1);
//    cout<<endl;
//    //00001011
//
//    k=(11<<8);
//
//    for(int i=15;i>=0;i--)
//    cout<<(k>>i&1);
//    cout<<endl;
//    //0000101100000000
//
//    return 0;
//}


void base64(char s[])
{
    char t[N]={0};//要加一个对数组的初始化。
    int l=strlen(s);
    int p=0;
    int a=0;

    int r=l%3;

    for(int i=0;i<l;i+=3)
    {
        int k=(((int)s[i])<<16)+(((int)s[i+1])<<8)+(int)(s[i+2]);//每次取三个字节
        int cb=1;

        for(int j=0;j<24;j++)
        {
            a+=(k>>j&1)*cb;
            cb<<=1;
            if(j%6==5)
            {
                t[p++]=e[a];
                a=0;
                cb=1;
            }
        }
        swap(t[p-1],t[p-4]);//4位字符是反的,比如AAA (100000101000001010000010)每6位一组后是BFUQ  倒过来后是QUFB 正常AAA应该是(010000010100000101000001)从左到右六个一组就是QUFB
        swap(t[p-2],t[p-3]);
    }

    if(r==1)
    t[p-1]=t[p-2]='=';
    if(r==2)
    t[p-1]='=';

    memcpy(s,t,sizeof(char)*N);
}

int main()
{
    int cas, n;
    scanf("%d", &cas);
    char s[N];
    for(int k = 1; k <= cas; ++k)
    {
        memset(s,0,sizeof(s));
        scanf("%d%s", &n, s);
        while(n--)
            base64(s);
        printf("Case #%d: %s\n", k, s);
    }
    return 0;
}
/*
0123456789012345678901234567890123456789012345678901234567890123
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
*/








HDU 5237 Base64

标签:

原文地址:http://blog.csdn.net/wust_zjx/article/details/46361479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!