HDU

来源:互联网 发布:java开发英文简历 编辑:程序博客网 时间:2024/06/03 19:44


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
21 Mike4 Mike
Sample Output
Case #1: TWlrZQ==Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==
Hint


当你看到这样长的模拟题的时候是不是感到一点恶心,不过好在哥的英语还不是很差,题目我来读,代码ps敲,代码有点丑,没我写的漂亮偷笑,不过思路还算清晰。。。


粘贴一下网友的题意:

 每一次的变换方法为:1.将字符串分为没三个字符一组;2.对于一组的每个字符,将其ASCII码值改写为二进制,不够八位的在前面补0(如果每次都对数组清空后从后面填数字,就不必考虑前面加0的情况);3.对于得到的24位数组,将其每6个字符分为一组,每组的6位变换为十进制,每个十进制数字对应一个字符(对应关系用字符串就可以解决);4.按顺序将变换之后得到的字符输出;5.对于字符串长度不能被三整除的,变为二进制后,对于不满八位的地方,在后面补0,剩下的不够四个字符就用等号补满;


#include <bits/stdc++.h>using namespace std;char tab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";int main(){    //printf("%d %d %d %d\n", 'M', 'i', 'k', 'e');    int t, k;    unsigned char s[1024];    int i;    int len;    unsigned char a, b, c, d;    unsigned char s2[1024];    int cas = 0;    int j;    int tot;    //char    scanf("%d", &t);    while (t--) {        scanf("%d%s", &k, s);        len = strlen((const char *)s);        printf("Case #%d: ", ++cas);        for (j = 0; j < k; ++j) {            tot = 0;            for (i = 0; i + 2 < len; i += 3) {                a = s[i] >> 2;                b = s[i] << 6, b >>= 2, b += s[i + 1] >> 4;                c = s[i + 1] << 4, c >>= 2, c += s[i + 2] >> 6;                d = s[i + 2] << 2, d >>= 2;                //printf("%d %d %d %d\n", a, b, c, d);                //printf("%c%c%c%c", tab[a], tab[b], tab[c], tab[d]);                s2[tot++] = tab[a];                s2[tot++] = tab[b];                s2[tot++] = tab[c];                s2[tot++] = tab[d];            }            if (i == len - 1) {                a = s[i] >> 2;                b = s[i] << 6, b >>= 2;                //printf("%c%c==\n", tab[a], tab[b]);                s2[tot++] = tab[a];                s2[tot++] = tab[b];                s2[tot++] = '=';                s2[tot++] = '=';            } else if (i == len - 2) {                a = s[i] >> 2;                b = s[i] << 6, b >>= 2, b += s[i + 1] >> 4;                c = s[i + 1] << 4, c >>= 2;                //printf("%c%c%c=\n", tab[a], tab[b], tab[c]);                s2[tot++] = tab[a];                s2[tot++] = tab[b];                s2[tot++] = tab[c];                s2[tot++] = '=';            }            s2[tot] = '\0';            //printf("%s\n", s2);            strcpy((char *)s, (const char *)s2);            len = tot;            //printf("s = %s\n", s);        }        printf("%s\n", s2);    }    return 0;}





0 0
原创粉丝点击