问题1446--Base64 Coding

1446: Base64 Coding

时间限制: 1 Sec  内存限制: 32 MB
提交: 14  解决: 3
[状态] [讨论版] [提交] [命题人:]
题目描述


    Base64 is an encoding algorithm. It works by turning characters whose ASCII code is between 0x00 and 0xff into strings of only lower case letters, upper case letters and digits, thus avoiding the encoding ambiguous problem.
Let's take a look at a simple example.
 
Before transformation: 10101101 10111010 01110110
After transformation: 00101011 00011011 00101001 00110110
 
Their values in base 10 after transformation are 43, 27, 41 and 54, and the corresponding characters are 'r','b','p'and '2'. So we say that the Base64 Encoding of the 24-bit binary string is 'rbp2'. The decoding algorithm is to simply erase two prefix 0s of each byte and then concatenate them together.
But, if the number of bytes of the original text is not a multiple of 3, what should be done? The solutions is we will add redundant 0s, and the Base64 Encoding will use '=' to indicate these 0s. That¡¯s why some Base64 encoded strings are ended with one or two '='s.
If the resulting string is too long, place 76 characters per line.
 
Consider the famous sentence from Thomas Hobbes¡¯s Leviathan.
Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge exceeds the short vehemence of any carnal pleasure.
After Base64 encoded, it is as follows.
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
 
Now, you are to encode or decode the message.

输入

    There are multiple test cases. The test cases are ended with "#exit", which should not be processed.

Each test case begins with a line containing "#b2s#" or "#s2b#", indicating to perform decoding or encoding operation on the input string. And every test case is ended by "#CaseEnd#". The string in between is the characters to be processed. The length of string is less than 10^6

输出

 Print "Case #id" on a single line where "id"is the case number numbered from 1 and then output the resulting string.

样例输入 Copy
#s2b#
C^$AL6N20)QQ[IKSF;\',^9D>-<&W'>S2-(#[+F?#:
#CaseEnd#
#b2s#
NVA6PV4xRU5XQSJSPD0nVzRFRlYhPSE4WQ==
#CaseEnd#
#exit#
样例输出 Copy
Case #1
Q14kQUw2TjIwKVFRW0lLU0Y7XCcsXjlEPi08JlcnPlMyLSgjWytGPyM6
Case #2
5P:=^1ENWA"R<='W4EFV!=!8Y
提示

The newline characters after ¡°#b2s#¡± or ¡°#s2b#¡± and before ¡°#CaseEnd#¡± don¡¯t need to be processed. Others are regarded as ordinary characters.
The base64 encoding table:
0    A
1    B
2    C
3    D
4    E
5    F
6    G
7    H
8    I
9    J
10    K
11    L
12    M
13    N
14    O
15    P
16    Q
17    R
18    S
19    T
20    U
21    V
22    W
23    X
24    Y
25    Z
26    a
27    b
28    c
29    d
30    e
31    f
32    g
33    h
34    i
35    j
36    k
37    l
38    m
39    n
40    o
41    p
42    q
43    r
44    s
45    t
46    u
47    v
48    w
49    x
50    y
51    z
52    0
53    1
54    2
55    3
56    4
57    5
58    6
59    7
60    8
61    9
62    +
63    /

来源/分类