Base64

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 245    Accepted Submission(s): 119

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 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,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 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==

题意:将3个8位变成4个6位。不足八位的时候补0,最后不足四位补=。求出k次变换后的结果

#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
char e[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; //int main()
//{
// int k=11;
// for(int i=7;i>=0;i--)
// cout<<(k>>i&1);
// cout<<endl;
// //00001011
//
// k=(11<<8);
//
// for(int i=15;i>=0;i--)
// cout<<(k>>i&1);
// cout<<endl;
// //0000101100000000
//
// return 0;
//} void base64(char s[])
{
char t[N]={0};//要加一个对数组的初始化。
int l=strlen(s);
int p=0;
int a=0; int r=l%3; for(int i=0;i<l;i+=3)
{
int k=(((int)s[i])<<16)+(((int)s[i+1])<<8)+(int)(s[i+2]);//每次取三个字节
int cb=1; for(int j=0;j<24;j++)
{
a+=(k>>j&1)*cb;
cb<<=1;
if(j%6==5)
{
t[p++]=e[a];
a=0;
cb=1;
}
}
swap(t[p-1],t[p-4]);//4位字符是反的,比方AAA (100000101000001010000010)每6位一组后是BFUQ 倒过来后是QUFB 正常AAA应该是(010000010100000101000001)从左到右六个一组就是QUFB
        swap(t[p-2],t[p-3]);
} if(r==1)
t[p-1]=t[p-2]='=';
if(r==2)
t[p-1]='='; memcpy(s,t,sizeof(char)*N);
} int main()
{
int cas, n;
scanf("%d", &cas);
char s[N];
for(int k = 1; k <= cas; ++k)
{
memset(s,0,sizeof(s));
scanf("%d%s", &n, s);
while(n--)
base64(s);
printf("Case #%d: %s\n", k, s);
}
return 0;
}
/*
0123456789012345678901234567890123456789012345678901234567890123
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
*/

HDU 5237 Base64的更多相关文章

  1. hdu 5237 Base64(模拟)

    Base64 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  2. HDU 5237 Base64 模拟

    题意: 输入一个明文串,输出\(k\)次\(Base64\)加密以后得到的串. 分析: 好像没什么Trick,直接模拟就行了. 注意:长度为\(3k+1\)的串,后面会有两个\(=\).长度为\(3k ...

  3. hdu 5237 二进制

    很无聊的模拟题...mark几个有用的小程序: 字符->二进制ASCII码 string tobin(char c) { string t; ; i<; i++) { t=+)+t; c/ ...

  4. 数据结构:HDU 2993 MAX Average Problem

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. JS base64 加密和 后台 base64解密(防止中文乱码)

    直接上代码 1,js(2个文件,网上找的)  不要觉的长,直接复制下来就OK //UnicodeAnsi.js文件 //把Unicode转成Ansi和把Ansi转换成Unicode function ...

  6. HDU 5734 Acperience

    Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. hdu 3038(扩展并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:给出区间[1,n],下面有m组数据,l r v区间[l,r]之和为v,每输入一组数据,判断 ...

  8. Base64 JAVA后台编码与JS前台解码(解决中文乱码问题)

    中文通过Java后台进行Base64编码后传到前台,通过JS进行Base64解码时会出现中文乱码的问题,被这个问题也是困扰了几天,使用jquery.base64.js只能转码非中文字符,经过搜集各种方 ...

  9. hdu 2461(AC) & poj 3695(TLE)(离散化+矩形并)

    Rectangles Time Limit: 5000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. android-开发环境相关概念

    Android中IDE.ADT.SDK.JDK.NDK的解释 1. IDE: Intelligent Development Environm的简称.即智能开发环境.是一种开发工具.常用的IDE有ad ...

  2. smarty课程---最最最简单的smarty例子

    smarty课程---最最最简单的smarty例子 一.总结 一句话总结:其实所有的模板引擎的工作原理是差不多的,无非就是在php程序里面用正则匹配将模板里面的标签替换为php代码从而将两者混合为一个 ...

  3. 酉矩阵(unitary matrix)

    复方阵 U 称为酉矩阵,如果满足: U∗U=UU∗=I 换句话说,矩阵 U 的共轭转置 U∗ 就是 U 的逆矩阵. U∗=U−1 1. unitary matrix 保持内积不变 ⟨Ux,Uy⟩=⟨x ...

  4. css中margin上下外边距重叠问题

    css的盒子模型里是这样规定两个对象之间的距离的:对象之间的间距是由两个对象的盒子模型的最终计算值得出来的,也就是说两个对象之间的间距就是两个对象的距离,但是当遇到两个对象一个有下外边距margin, ...

  5. Spring_Learn

    IOC  控制反转,或者依赖注入  控制权的转移,应用程序本身不负责依赖对象的创建和维护.而是由外部容器负责创建和维护. DI(依赖注入)是其实现的一种方式 创建对象并且组装对象之间的关系. 1Spr ...

  6. Gym 100952 F. Contestants Ranking

    http://codeforces.com/gym/100952/problem/F F. Contestants Ranking time limit per test 1 second memor ...

  7. Linux之lldptool命令

    1. 描述 当我们想在操作系统里面查看网口和交换机连接的状态信息,我们可以使用lldptool这个工具. 2.LLDP协议 LLDP是一个数据链路层发现协议,LLDP协议使得接入网络的一台设备可以将其 ...

  8. echo---打印变量或输出字符串

    cho命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下e ...

  9. php中ajax使用实例

    php中ajax使用实例 一.总结 1.多复习:这两段代码都挺简单的,就是需要复习,要多看 2.ajax原理:ajax就是部分更新页面,其实还在的html页面监听到事件后,然后传给服务器进行操作,这里 ...

  10. 八、Docker+RabbitMQ

    原文:八.Docker+RabbitMQ 一.下载镜像 docker pull rabbitmq:management 二.运行 docker run -d --name rabbitmq -e TZ ...