HDU
来源:互联网 发布:淘宝商品7天自动下架 编辑:程序博客网 时间:2024/06/14 06:23
点击打开题目链接
Base64
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1285 Accepted Submission(s): 578
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==
Source
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest
Recommend
We have carefully selected several similar problems for you: 6022 6021 6020 6019 6018
白天五一训练赛,晚上要去建模,都快没时间更新博客了(TAT)
题目大意:将字符串转化为ASCII码的二进制,然后每6个二进制位组成新的10进制数字,求得它的ASCII对应的字符串。
思路:将各种功能分成单个函数实现。
附上AC代码:
#include<iostream>#include<string>#include<algorithm>using namespace std;char base_64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";int T;int n;string s;int kase;//将单个字符转化为8位二进制字符string ctobinary(char c){ string binary; int temp=c; for(int i=0;i<8;i++) { binary+=temp%2+'0'; temp/=2; } reverse(binary.begin(),binary.end()); return binary;}//将每6个二进制转为10进制后转为字符char binarytoc(string st,int t){ char c; int temp=0; for(int i=t;i<t+6;i++) temp=temp*2+st[i]-'0'; c=base_64[temp]; return c;}//将字符串转化为二进制string stobinary(string st){ string ans; for(int i=0;i<(int)st.length();i++) { ans+=ctobinary(st[i]); } return ans;}string solve(string st){ int num; //每六个二进制位组合成新的字符串 string ans=stobinary(st); if((int)ans.length()%6!=0) { num=6-(int)ans.length()%6; for(int i=0;i<num;i++) ans+='0'; } string sum; for(int i=0;i<(int)ans.length();i+=6) { sum+=binarytoc(ans,i); } if((int)sum.length()%4!=0) { num=4-(int)sum.length()%4; for(int i=0;i<num;i++) sum+='='; } return sum;}int main(){ ios::sync_with_stdio(false); while(cin>>T) { kase=0; while(T--) { cin>>n>>s; cout<<"Case #"<<++kase<<": "; while(n--) { s=solve(s);// str=stobinary(s); } cout<<s<<endl; } } return 0;}
2 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- softmax 与 logistic Regression
- 商城项目实战 | 6.1 OkHttp 的详细介绍 网络请求更加简单
- kettle linux 下的使用
- 从程序员到项目经理:完美主义也是一种错
- iOS手势冲突问题
- HDU
- c++实验5
- 异常:calling prepareStatement is no longer allowed! Increase reapTimeout to avoid this problem
- jquery学习随笔
- 设计模式08:装饰者模式
- XCode免升级支持iOS设备调试
- POJ 3013:Big Christmas Tree
- iOS开发-Xcode编译常见错误解决:linker command failed with exit code 1 (use -v to see invocation)
- IOS_UIWebView字体大小、字体颜色、背景色