HDU 5237-Base64(模擬-K輪加密)
Base64
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1242 Accepted Submission(s): 556
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==.
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.
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
題目意思:
T組測試資料,每組K輪加密S字串。
先根據每個字元的ASCII碼轉換成八位二進位制,然後分割成六位二進位制碼轉換回ASCII,如果末尾不足六位需要補零,如果當前輪加密後的字串長度不是四的倍數,需要在末尾補‘#’。
解題思路:
模擬…注意處理好細節就好啦~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <map>
#include <iomanip>
#include <algorithm>
#define MAXN 100000
#define INF 0xfffffff
using namespace std;
char c[MAXN];
int a[MAXN];
string str[MAXN];
char ans[MAXN];
char b[MAXN];
char f[65]= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};//加密規則
void solve(char c[])
{
memset(a,'\0',sizeof(a));//儲存ASCII碼
memset(b,'\0',sizeof(b));//儲存二進位制串
memset(ans,'\0',sizeof(ans));
int len=strlen(c);
int cnt=0,cans=0;
for(int i=0; i<len; ++i)
{
a[i]=int(c[i]);
bitset<8>s(a[i]);//轉換成八位二進位制串
string c=s.to_string<char,char_traits<char>,allocator<char> >();
for(int j=0; j<8; ++j) b[cnt++]=c[j];
}
int l=0,r=0,lenb;
if(cnt%6!=0) l=cnt,r=((cnt/6)+1)*6;//計算需要末尾補零時
bool bl=false;//標誌是否進行補零操作
if(r!=0&&l!=0)
{
bl=true;
for(int i=l; i<r; ++i)
b[i]='0';//補零
lenb=r;//更新b陣列長度
}
else lenb=cnt;
l=0,r=0;
for(int i=0; i<cnt; ++i)
{
if(i!=0&&(i+1)%6==0)//每六位一組計算
{
r=i;//當前六位陣列的起始下標
char ch[10];
int cn=0;
for(int j=l; j<=r; ++j)
ch[cn++]=b[j];
string str2(ch);
bitset<6> bs2(str2);//將六位二進位制數轉換成ASCII碼
int res=bs2.to_ulong();
ans[cans++]=f[res];
l=i+1;//當前六位陣列的結束下標
}
}
if(bl)//計算補零後最後六位
{
char ch[10]= {'\0'};
int cn=0;
for(int i=l; i<lenb; ++i)
ch[cn++]=b[i];
string str2(ch);
bitset<6> bs2(str2);
int res=bs2.to_ulong();
ans[cans++]=f[res];//加密後的字元存入陣列
}
if(cans%4!=0) l=cans,r=((cans/4)+1)*4;//計算需要末尾補‘#’時
for(int i=l; i<r; ++i)
ans[cans++]='=';
/* for(int i=0; i<cans; ++i)
cout<<ans[i];
cout<<endl;*/
memset(c,'\0',sizeof(c));
for(int i=0; i<cans; ++i)//本次計算得到的加密串複製到原串c以便下次加密
c[i]=ans[i];
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("G:/cbx/read.txt","r",stdin);
//freopen("G:/cbx/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int T,ca=0;
cin>>T;
while(T--)
{
int k;
cin>>k;
memset(c,'\0',sizeof(c));
cin>>c;
for(int i=0; i<k; ++i)//k次加密
solve(c);
int len=strlen(c);
cout<<"Case #"<<++ca<<": ";
for(int i=0; i<len; ++i)//輸出結果
cout<<c[i];
cout<<endl;
}
return 0;
}
相關文章
- HDU 4288-Coder(模擬)
- YT15-HDU-字串的模擬字串
- HTML5模擬齒輪動畫HTML動畫
- ORACLE透明加密場景模擬Oracle加密
- HDU 4985-Little Pony and Permutation(模擬置換)
- HDU 5319 Painter (模擬 腦洞題)AI
- HDU 6015-Skip the Class(模擬-結構體排序)結構體排序
- 2013杭州網路賽C題HDU 4640(模擬)
- CSP-J 第一輪 2024模擬卷-1
- NBA 2K23 Arcade Edition for Mac(籃球模擬遊戲)Mac遊戲
- 模擬
- 基於simulink的風輪機發電系統建模與模擬
- 2014北京網路賽1007||hdu5038 模擬
- Python自動化神器:如何用PyAutoGUI模擬滾輪動一次PythonGUI
- 有限元模擬 有限體積模擬
- 10.6 模擬賽(NOIP 模擬賽 #9)
- 2012 天津站B題||hdu4432 進位制轉換 模擬
- git 模擬Git
- ACP模擬
- 模擬題
- Keil的軟體模擬和硬體模擬
- Thinking in Java---多執行緒模擬:銀行出納員模擬+飯店模擬+汽車裝配工廠模擬ThinkingJava執行緒
- Gpssworld模擬(二):並排排隊系統模擬
- Altair SimSolid 工程模擬軟體 衡祖模擬AISolid
- PID除錯軟體(C#、模擬、模擬)除錯C#
- 「模擬賽」暑期集訓CSP提高模擬10(7.28)
- 「模擬賽」暑期集訓CSP提高模擬15(8.7)
- NOIP模擬50
- NOIP模擬57
- iOS 模擬器iOS
- NOIP模擬74
- NOIP模擬76
- NOIP模擬77
- NOIP模擬66
- 模擬面試題面試題
- 模擬退火原理
- 5.4 模擬賽
- 模擬鬥地主