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 \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 84, 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 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 010101, 000110, 100001 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(T≤20) denoting the number of test cases.
  
  In the following T lines, each line contains a case. In each case, there is a number k(1≤k≤5) 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==
 
Source
 
 
这么长的题目我没读,
 #include <bits/stdc++.h>
using namespace std; char tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int main()
{ //printf("%d %d %d %d\n", 'M', 'i', 'k', 'e');
int t, k;
unsigned char s[];
int i;
int len;
unsigned char a, b, c, d;
unsigned char s2[];
int cas = ;
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 = ; j < k; ++j) {
tot = ;
for (i = ; i + < len; i += ) {
a = s[i] >> ;
b = s[i] << , b >>= , b += s[i + ] >> ;
c = s[i + ] << , c >>= , c += s[i + ] >> ;
d = s[i + ] << , d >>= ;
//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 - ) {
a = s[i] >> ;
b = s[i] << , b >>= ;
//printf("%c%c==\n", tab[a], tab[b]);
s2[tot++] = tab[a];
s2[tot++] = tab[b];
s2[tot++] = '=';
s2[tot++] = '=';
} else if (i == len - ) {
a = s[i] >> ;
b = s[i] << , b >>= , b += s[i + ] >> ;
c = s[i + ] << , c >>= ;
//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 ;
}

最新文章

  1. C# BS消息推送 负载均衡-SignalR&amp;Redis的配置(三)
  2. PDF 补丁丁 0.5.0.2691 发布(替换字库新增字符映射功能)
  3. python学习笔记12 ----线程、进程
  4. SWFTools参数
  5. HDOJ 1754 I Hate It 线段树 第二题
  6. [转载] C++ string, const char*, char* 之间互相转换
  7. UIPickView之自定义生日键盘和城市键盘
  8. DML数据操作语言之增加,删除,更新
  9. C语言程序设计第一次作业 冯亚杰
  10. JavaScript判断不同平台
  11. 分享收集的WebGL 3D学习资源
  12. 【BZOJ5416】【NOI2018】冒泡排序(动态规划)
  13. (原)ubuntu中使用conda安装tensorflow-gpu
  14. 微信小程序之用户信息授权 wx.getUserInfo
  15. div+Css绝对定位(absolute)和相对定位(relative)的总结
  16. 用 Love2D 实现法线贴图的例程(到最新版本 0.10.1)
  17. day07作业
  18. Android Activity 半透明效果(Translucent)
  19. C. Ayoub and Lost Array Round #533 (Div. 2) 【DP】
  20. OpenGL总结

热门文章

  1. django startapp报 maximum recursion depth exceeded
  2. window.onload和$(document).ready()比较
  3. open-falcon 前端代码在windows上运行
  4. C# 函数2
  5. Shallow Copy &amp; Deep Copy in Python list
  6. C#如何打印RichTextBox控件的内容
  7. 源码编译搭建LAMP
  8. Django基础(二)_Ajax、csrf伪站请求
  9. PAT 天梯赛 L1-029. 是不是太胖了 【水】
  10. macOS 简单使用