Skip to content

Instantly share code, notes, and snippets.

@ducphanduyagentp
Created November 8, 2014 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducphanduyagentp/a288abe908a36e5baa79 to your computer and use it in GitHub Desktop.
Save ducphanduyagentp/a288abe908a36e5baa79 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define f0(i,n) for(__typeof(n) i=0;i<n;i++)
#define rep(i,a,b) for(__typeof(b) i=a;i<=b;i++)
#define read(s) freopen(s , "r" ,stdin)
#define write(s) freopen(s , "w" , stdout)
#define BUG(x) cerr << #x << " = " << x << '\n'
using namespace std;
const string pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string encode(string s){
string res;
f0(i,(int)s.length()){
int k = (int)s[i];
string tmp;
f0(j,8){
char cc;
if (k & (1<<j)) cc = '1';
else cc = '0';
tmp.insert(tmp.begin(),cc);
}
res += tmp;
}
return res;
}
int main(){
string s;
string res;
getline(cin,s);
string tmp = encode(s);
int remainder = 3 - (int)s.length() % 3;
string add = "";
if ( remainder < 3 ){
f0(i,remainder) add += '=' , tmp += "00000000";
f0(i,6*remainder) tmp.erase(tmp.end()-1);
}
for ( int i = 0 ; i < (int)tmp.length() ; i += 6 ){
int num = 0;
for ( int j = 5 ; j >= 0 ; j -- ) num |= ((int)(tmp[i+5-j]-'0'))<<j;
res += pattern[num];
}
res += add;
puts(res.c_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment