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 ;
}

hdu 5237 Base64(模拟)的更多相关文章

  1. HDU 5237 Base64 模拟

    题意: 输入一个明文串,输出\(k\)次\(Base64\)加密以后得到的串. 分析: 好像没什么Trick,直接模拟就行了. 注意:长度为\(3k+1\)的串,后面会有两个\(=\).长度为\(3k ...

  2. HDU 5237 Base64

    Base64 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  3. HDU 4121 Xiangqi 模拟题

    Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...

  4. hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...

  5. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  6. HDU 2568[前进]模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2568 关键思想:傻傻地模拟 代码如下: #include<iostream> using ...

  7. hdu 4964 恶心模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=4964 给定语句,按照语法翻译html并输出. 就是恶心的模拟,递归搞就行了 处理id和class时,在一个'&g ...

  8. HDU 5965 枚举模拟 + dp(?)

    ccpc合肥站的重现...一看就觉得是dp 然后强行搞出来一个转移方程 即 根据第i-1列的需求和i-1 i-2列的枚举摆放 可以得出i列摆放的种类..加了n多if语句...最后感觉怎么都能过了..然 ...

  9. hdu 5237 二进制

    很无聊的模拟题...mark几个有用的小程序: 字符->二进制ASCII码 string tobin(char c) { string t; ; i<; i++) { t=+)+t; c/ ...

随机推荐

  1. 000-mysql小技巧

    1.使用Navicat 链接5.7版本出现 mysql 5.7.9 [Err] 1055报错解决,[Err] 1055 – Expression #1 of ORDER BY clause is no ...

  2. ABAP重点各种接口技术

    转自 http://www.cnblogs.com/penley/archive/2008/11/12/1332140.html 下面总结一下ABAP中的各种接口技术,因为学习时间不是很长,肯定还不全 ...

  3. MySQL数据库(1)_MySQL数据库介绍与安装

    一.数据库相关概念的简介 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展 ...

  4. Python进阶(4)_进程与线程 (python并发编程之多进程)

    一.python并发编程之多进程 1.1 multiprocessing模块介绍 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大 ...

  5. loadrunder之脚本篇——Run-time Settings之Pacing

      As soon as the previous iteration ends 前一个迭代一结束就尽可能快的开始新一轮的迭代   After the previous iteration ends ...

  6. linux mint —— 图片一张

    概述 Linux Mint是一種基於Ubuntu開發出的Linux操作系统.由Linux Mint Team团队于2006年开始发行.Linux Mint 的目标是为家庭用户和企业客户提供一个免费.高 ...

  7. $《第一行代码:Android》读书笔记——第6章 数据持久化

    主要讲述了Android数据持久化的三种方式:文件存储.SharedPreference存储.SQLite数据库存储. (一)文件存储 其实Android中文件存储方式和Java的文件操作类似,就是用 ...

  8. I.mx6s上移植wm8960驱动(基于linux3.0.101版本)

    I.mx6s上移植wm8960驱动   此篇博文只记录移植的步骤,其他不做分析.首先上一张wm8960的硬件连接图: 1  上电操作   配置wm8960的上电脚,文件位置:arch/arm/mach ...

  9. STM32探秘 之FSMC

    源:STM32探秘 之FSMC STM32 FSMC总线深入研究

  10. Django框架之自定义分页

    分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输入页码(第一页.第二页...) 3.根据设定的每页显示条数和当 ...