Base64 (HDU - 5237)

点击打开原题链接



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 84 104 104, and  101 101, which are the  8 8-bit binary values  01010100 01010100 01101000 01101000, and  01100101 01100101. These three values are joined together into a 24-bit string, producing  010101000110100001100101 010101000110100001100101
Groups of  6 6 bits ( 6 6 bits have a maximum of  26=64 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 010101000110100001100101 is divided into four parts  010101 010101 000110 000110 100001 100001 and  100101 100101, and converted into integers  21,6,33 21,6,33and  37 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 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( T20 T≤20) denoting the number of test cases. 
   
  In the following  T T lines, each line contains a case. In each case, there is a number  k(1k5) k(1≤k≤5) and a string  s s s s only contains characters whose ASCII value are from  33 33 to  126 126(all visible characters). The length of  s s is no larger than  100 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==


题意:算是很水的一个题,题目很长,吓人一跳,大体意思就是给你一个字符串,写出每个字符的二进制(不组八位的前面补0),然后依次把每个字符的二进制01串加起来,然后如果原字符串的长度%3==1的话,那么01串后面补四个零;如果原字符串的长度%3==2,那么01串后边补上2个0。最后解码。

详见代码:

#include<cstdio>
#include<cstring>
#include<string> 
#include<algorithm>
#include<iostream> 
using namespace std;
char code[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string op[100]={"00100001","00100010","00100011","00100100","00100101","00100110","00100111","00101000","00101001","00101010","00101011","00101100","00101101","00101110","00101111","00110000","00110001","00110010","00110011","00110100","00110101","00110110","00110111","00111000","00111001","00111010","00111011","00111100","00111101","00111110","00111111","01000000","01000001","01000010","01000011","01000100","01000101","01000110","01000111","01001000","01001001","01001010","01001011","01001100","01001101","01001110","01001111","01010000","01010001","01010010","01010011","01010100","01010101","01010110","01010111","01011000","01011001","01011010","01011011","01011100","01011101","01011110","01011111","01100000","01100001","01100010","01100011","01100100","01100101","01100110","01100111","01101000","01101001","01101010","01101011","01101100","01101101","01101110","01101111","01110000","01110001","01110010","01110011","01110100","01110101","01110110","01110111","01111000","01111001","01111010","01111011","01111100","01111101","01111110"};
string in;
int main()
{
   /*char cc[200];
   for(int i=33;i<=126;i++)
   {
     char pp[100];
   	 int sum = 0;
   	 int t=i;
   	 while(t)
   	 {
   	  char c;
   	  if(t%2==0) c = '0';
   	  else c = '1';
	  pp[sum] = c;
   	   t /= 2;
   	   sum++;
	 }
	 while(sum<8)
	 {
	 	pp[sum] = '0';
	 	sum++;
	 }
	  reverse(pp,pp+8);
     pp[8] = '\0';
   	 printf("\"");
   	 printf("%s",pp);
   	 printf("\"");
   	 printf(",");
   	
   	
   }*/
   // 以上为打表专用 ,提前求出八位的二进制,存在op数组中。op[0]存储的是ascii表中 值为33的字符,往后依次递加。 
   
   int T;
   scanf("%d",&T);
   for(int rr=1;rr<=T;rr++)
   {
   	
   int k;
   scanf("%d",&k);
   string sum;
   string bu;
   cin >> in;
   //cout << in << endl;
   string fuin;
   fuin="";
   sum = "";
   for(int w=0;w<k;w++)
{
   int len = in.size();
   //cout << len << endl; 
   if(len%3==1)
   {
   	 bu = "==";
   	 for(int i=0;i<len;i++)
   	 {
   	   sum += op[(int)in[i]-33];	
	 }
	 for(int i=1;i<=4;i++)
	 {
	 	sum += "0";
	 }
	 
   } 
   else if(len%3==2)
   {
   	 bu = "=";
   	  for(int i=0;i<len;i++)
   	 {
   	   sum += op[(int)in[i]-33];	
	 }
	 for(int i=1;i<=2;i++)
	 {
	 	sum += "0";
	 }
	 
   }
   
   else if(len%3==0)
   {
   	  bu = "";
   	  for(int i=0;i<len;i++)
   	  {
   	   sum += op[(int)in[i]-33];	
	  }
   }
   int p = 0;
   int ttt = sum.size();
   while(p<ttt)
   {
   	 int num = 0;
   	 int er = 1;
	for(int i=p+5;i>=p;i--)
   	{
   	   int e = sum[i] - '0';
	   num += er*e;
   	   er *= 2;
   	}
   	//cout << num << endl;
   	fuin += code[num];
   	p += 6;
   }
  in = fuin+bu;
  fuin = "";
  sum = "";
  //cout << in << endl;
}
  printf("Case #%d: ",rr);
  cout << in << endl;	
	
}
	return 0;
}

水波。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值