zoukankan      html  css  js  c++  java
  • hdu 5237 Base64(模拟)

    Base64

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


    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 exttt{Base64}, it looks as follows

    VGhlIE1pbmlzdHJ5IG9mIEZvcmVpZ24gQWZmYWlycyBvZiB0aGUgUGVvcGxlJ3MgUmVwdWJsaWMgb2Yg
    Q2hpbmEgcmVxdWVzdHMgYWxsIGNpdmlsIGFuZCBtaWxpdGFyeSBhdXRob3JpdGllcyBvZiBmb3JlaWdu
    IGNvdW50cmllcyB0byBhbGxvdyB0aGUgYmVhcmVyIG9mIHRoaXMgcGFzc3BvcnQgdG8gcGFzcyBmcmVl
    bHkgYW5kIGFmZm9yZCBhc3Npc3RhbmNlIGluIGNhc2Ugb2YgbmVlZC4=

    In the above text, the encoded result of exttt{The} is exttt{VGhl}. Encoded in ASCII, the characters exttt{T}, exttt{h}, and exttt{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,33and 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
     
     
    这么长的题目我没读,
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 char tab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     5 
     6 int main()
     7 {
     8 
     9     //printf("%d %d %d %d
    ", 'M', 'i', 'k', 'e');
    10     int t, k;
    11     unsigned char s[1024];
    12     int i;
    13     int len;
    14     unsigned char a, b, c, d;
    15     unsigned char s2[1024];
    16     int cas = 0;
    17     int j;
    18     int tot;
    19     //char
    20 
    21     scanf("%d", &t);
    22 
    23     while (t--) {
    24         scanf("%d%s", &k, s);
    25         len = strlen((const char *)s);
    26         printf("Case #%d: ", ++cas);
    27 
    28         for (j = 0; j < k; ++j) {
    29             tot = 0;
    30             for (i = 0; i + 2 < len; i += 3) {
    31                 a = s[i] >> 2;
    32                 b = s[i] << 6, b >>= 2, b += s[i + 1] >> 4;
    33                 c = s[i + 1] << 4, c >>= 2, c += s[i + 2] >> 6;
    34                 d = s[i + 2] << 2, d >>= 2;
    35                 //printf("%d %d %d %d
    ", a, b, c, d);
    36                 //printf("%c%c%c%c", tab[a], tab[b], tab[c], tab[d]);
    37                 s2[tot++] = tab[a];
    38                 s2[tot++] = tab[b];
    39                 s2[tot++] = tab[c];
    40                 s2[tot++] = tab[d];
    41             }
    42             if (i == len - 1) {
    43                 a = s[i] >> 2;
    44                 b = s[i] << 6, b >>= 2;
    45                 //printf("%c%c==
    ", tab[a], tab[b]);
    46                 s2[tot++] = tab[a];
    47                 s2[tot++] = tab[b];
    48                 s2[tot++] = '=';
    49                 s2[tot++] = '=';
    50             } else if (i == len - 2) {
    51                 a = s[i] >> 2;
    52                 b = s[i] << 6, b >>= 2, b += s[i + 1] >> 4;
    53                 c = s[i + 1] << 4, c >>= 2;
    54                 //printf("%c%c%c=
    ", tab[a], tab[b], tab[c]);
    55                 s2[tot++] = tab[a];
    56                 s2[tot++] = tab[b];
    57                 s2[tot++] = tab[c];
    58                 s2[tot++] = '=';
    59             }
    60             s2[tot] = '';
    61             //printf("%s
    ", s2);
    62             strcpy((char *)s, (const char *)s2);
    63             len = tot;
    64             //printf("s = %s
    ", s);
    65         }
    66         printf("%s
    ", s2);
    67     }
    68 
    69     return 0;
    70 }
  • 相关阅读:
    数据分析英国电商——数据分析可视化
    数据分析英国电商——数据预处理部分
    特征工程入门与实践—3 特征增强
    特征工程入门与实践—2 特征理解
    特征工程入门与实践 —1 特征工程简介
    正则表达式匹配
    linux学习笔记
    python深度学习基础
    Linux命令小记1
    AWS S3操作命令
  • 原文地址:https://www.cnblogs.com/gongpixin/p/6786267.html
Copyright © 2011-2022 走看看