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. gearman background后台job状态获取

    GearmanClient background job有一个方法叫: public array GearmanClient::jobStatus ( string $job_handle ) Get ...

  2. input date 对 placeholder 的支持问题

    正常情况下,text 的 input 会显示 placeholder 中的值,date 类型的 input 对其支持不好.实例代码如下: <input type="text" ...

  3. 【转】Python的hasattr() getattr() setattr() 函数使用方法详解

    Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...

  4. 函数没有返回值,默认返回undefined

    var  a =( function(){return})(); a = undefined;

  5. DEC VT100 terminal

  6. Hibernate一对多、多对一关联

    一对多.多对一关联:在多方加外键 示例:Group(一方)和User(多方),一个Group可以有多个User,每个User只能属于一个Group   多对一单向关联 在User(多方)中建Group ...

  7. IEEE802.11数据帧在Linux上的抓取 80211格式转8023帧格式

    转:http://blog.csdn.net/dog250/article/details/7749372 终于得到了梦寐的<802.11无线网络权威指南>,虽然是复印版本,看起来也一样舒 ...

  8. 【Head First Servlets and JSP】笔记8:监听者

    1.你不用了解所有监听者API,并不多,一共有8个.不过,你需要知道你能监听什么,以便在需要的时候可以查. 2.关于Session和Cookie.参见JavaWeb学习总结(十二)——Session ...

  9. 使用ASP.Net MVC5 Web API OData和Sencha Touch 开发WebAPP

    使用ASP.Net MVC5 Web API OData和SenCha Touch 开发WebAPP Demo 效果 第一步 创建数据库 创建表 第二步 搭建MVC,并导入OData 第三步,写入We ...

  10. linux下的cacti安装(centos7)

    1 cacti运行环境准备 cacti需要php+apache+mysql+snmp+RRDTool,以及cacti本身.cacti本体是用php开发的网站,通过snmp对远端设备信息进行采集.apa ...