gdcpc2015B题
来源:互联网 发布:cf天赐软件视频 编辑:程序博客网 时间:2024/04/20 07:15
题目名称:Base64
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5237(在hdu上的地址)
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 bytes84 , 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 of6 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 string010101000110100001100101 is divided into four parts 010101 , 000110 , 100001 and 100101 , and converted into integers 21,6,33 and 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 fork times. Can you help him?
For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.
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
Groups of
0123456789012345678901234567890123456789012345678901234567890123
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
In the above example, the string
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
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 followingT 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 .
In the following
Output
For each test case, output Case #t:, to represent this is t-th case. And then output the encoded string.
Sample Input
21 Mike4 Mike
Sample Output
Case #1: TWlrZQ==Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==
代码如下:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<string>using namespace std;char a[]= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";int main(){ int t,k,cnt=1; while(scanf("%d",&t)!=EOF) { while(t--) { string s; cin>>k>>s; while(k--) { string bit=""; int len=s.length(); s[len]=s[len+1]=s[len+2]=0; //三个字符刚好转换,所以补足位数 for(int i=0;i<(len-1)/3+1;i++) { int z=(((int)s[3*i])<<16)+(((int)s[3*i+1])<<8)+(int)s[3*i+2]; //三个字符转换成24位 char d[5]=""; for(int j=3;j>=0;j--) { d[j]=a[z%64]; z>>=6; } bit+=d; } int r=bit.length(); if(len%3==1) //后面少两个字符,补两个'=' bit[r-1]=bit[r-2]='='; if(len%3==2) //后面少一个字符,补一个'=' bit[r-1]='='; s=bit; } printf("Case #%d: ",cnt++); cout<<s<<endl; } } return 0;}
0 0
- gdcpc2015B题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 题
- 算法基础----关于数据结构中树的相关算法总结探究
- SAT阅读中的常见文化词条
- LinuxShell知识总结
- 在android apk中调用@hide方法
- MFC全局函数AfxBeginThread 及其与CreateThread的区别
- gdcpc2015B题
- Kruskal 算法求最小生成树
- C/C++预处理运算符
- FragmentPagerAdapter 与FragmentStatePagerAdapter 的不同
- nginx + tomcat + redis问题记录
- Provisioning Profile
- OpenDaylight和ONOS控制器比较
- log4cplus的使用
- 关于SIGPIPE信号