HDU 5237 Base64 模拟

题意:

输入一个明文串,输出\(k\)\(Base64\)加密以后得到的串。

分析:

好像没什么Trick,直接模拟就行了。
注意:长度为\(3k+1\)的串,后面会有两个\(=\)。长度为\(3k+2\)的串后面有一个\(=\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;

const int maxl = 10000;
int a[maxl];

string s;

char code(int x) {
    if(x < 26) return 'A' + x;
    if(x < 52) return 'a' + x - 26;
    if(x < 62) return '0' + x - 52;
    if(x == 62) return '+';
    return '/';
}

string Base64(string s) {
    int l = s.length();
    memset(a, 0, sizeof(a));
    int tot = 0;
    for(int i = 0; i < l; i++) {
        char c = s[i];
        for(int j = 7; j >= 0; j--)
            a[tot++] = (c >> j) & 1;
    }
    string ans = "";
    for(int i = 0; i < tot; i += 6) {
        int x = 0;
        for(int j = 0; j < 6; j++)
            x = x * 2 + a[i + j];
        ans += code(x);
    }
    if(l % 3 == 1) { ans += '='; ans += '='; }
    else if(l % 3 == 2) ans += '=';
    return ans;
}

int main()
{
    int T; scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++) {
        int k; scanf("%d", &k);
        cin >> s;
        while(k--) s = Base64(s);
        printf("Case #%d: ", kase);
        cout << s << "\n";
    }

    return 0;
}
posted @ 2015-11-05 23:13  AOQNRMGYXLMV  阅读(134)  评论(0编辑  收藏  举报