HDU 5237 Base64

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+/
*/
时间: 2024-08-30 01:58:05

HDU 5237 Base64的相关文章

hdu 5237 Base64(模拟)

Base64 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1255    Accepted Submission(s): 564 Problem Description Mike does not want others to view his messages, so he find a encode method Base64.

hdu 5237 二进制

很无聊的模拟题...mark几个有用的小程序: 字符->二进制ASCII码 1 string tobin(char c) 2 { 3 string t; 4 for(int i=0; i<8; i++) 5 { 6 t=char(c%2+48)+t; 7 c/=2; 8 } 9 return t; 10 }

上海邀请赛 补题中

比赛的时候就出了5题,赛后发现其实很多题可以写,不知道为啥赛时这么逗比加坑队友,过两天做个总结 5236 Article 29.45%(134/455) 5237 Base64 41.67%(175/420)   5238 Calculator 43.85%(82/187) 5239 Doom 23.67%(138/583) 5240 Exam 50.61%(249/492) 5241 Friends 48.86%(215/440) 5242 Game 29.91%(195/652) 5243

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

直接上代码 1,js(2个文件,网上找的)  不要觉的长,直接复制下来就OK //UnicodeAnsi.js文件 //把Unicode转成Ansi和把Ansi转换成Unicode function UnicodeChr() { return '00A4,00A7,00A8,00B0,00B1,00B7,00D7,00E0,00E1,00E8,00E9,00EA,00EC,00ED,00F2,00F3,00F7,00F9,00FA,00FC,0101,0113,011B,012B,014D,01

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是