码迷,mamicode.com
首页 > 其他好文 > 详细

手速模拟题

时间:2015-05-25 16:41:30      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:二进制   acm-icpc   位运算   

这个要理解对于二进制位的处理,运用强大的位运算。

#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  inf  0x3f3f3f3f
#define  maxn (10+100000)
using namespace std;
typedef long long ll;

const int charset=10;
int n;int cas=1;
char in[1100];
char out[1100];
int base[1100];
char *table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void encode(int n){

    while(n--){
        int len = strlen(in);
        int tot=0,rr = len%3;
        len -= rr;
        for(int i = 0;i<len;i+=3){
            base[tot++] = in[i]>>2;
            base[tot++] = ((in[i] & 0x03) << 4) | (in[i+1] >>4);
            base[tot++] =((in[i+1] & 0x0f) <<2) | (in[i+2] >>6);
            base[tot++] = in[i+2] & 0x3f;
        }

        ///for(int i = 0;i<tot;i++)printf("%d ",base[i]);
        if(rr==1){
            base[tot++] = in[len]>>2;
            base[tot++] = ( in[len] & 0x03 ) << 4;
            base[tot++] = 100;
            base[tot++] = 100;
        }
        else if(rr==2){
            base[tot++] = in[len]>>2;
            base[tot++] = ((in[len] & 0x03) << 4) | (in[len+1] >>4);
            base[tot++] = (in[len+1] & 0x0f) << 2;
            base[tot++] = 100;
        }
        for(int i = 0;i<tot;i++)
            if(base[i]<64)
                in[i] = table[base[i]];
            else in[i] = ‘=‘;
        in[tot] = 0;

    }
    printf("Case #%d: %s\n",cas++,in);
}

int change(char ch) {
    if (ch >= ‘A‘ && ch <= ‘Z‘)
        return ch - ‘A‘;
    if (ch >= ‘a‘ && ch <= ‘z‘)
        return ch - ‘a‘ + 26;
    if (ch >= ‘0‘ && ch <= ‘9‘)
        return ch - ‘0‘ + 52;
    if (ch == ‘+‘)
        return 62;
    return 63;
}
void decode(int n){
    while(n--){
        int tot = 0;
        int len = strlen(in);
        while(len && in[len-1]==‘=‘) len--;
        int rr = len%4;
        len -= rr;
        for(int i = 0;i<len;i+=4){
            base[tot++] = (change(in[i])<<2) | (change(in[i+1])>>4);
            base[tot++] = ((change(in[i+1]) & 0x0f)<<4) | (change(in[i+2])>>2);
            base[tot++] = ((change(in[i+2]) & 0x03)<<6) | change(in[i+3]);
        }
        if(rr==1){
            base[tot++] = change(in[len])<<2;
        } else if(rr==2){
            base[tot++] = (change(in[len])<<2) | (change(in[len+1])>>4);
            base[tot++] = ((change(in[len+1]) & 0x0f)<<4) ;
        } else if(rr==3){
            base[tot++] = (change(in[len])<<2) | (change(in[len+1])>>4);
            base[tot++] = ((change(in[len+1]) & 0x0f)<<4) | (change(in[len+2])>>2);
            base[tot++] = ((change(in[len+2]) & 0x03)<<6) ;
        }
        for(int i = 0;i<tot;i++)
            in[i] = base[i];
        in[tot] = 0;
        printf("Case #%d: %s\n",cas++,in);
    }

}
void decodeplus(int n){

    while(n--){
        int tot = 0;
        int len = strlen(in);
        while(len && in[len-1]==‘=‘) len--;
        int tmp = 0;
        int cnt = 0;
        for(int i = 0;i<len;i++){
            cnt += 6;
            tmp = (tmp<<6) | change(in[i]);
            if(cnt >=8){
                base[tot++] = (tmp >> cnt-8) & 0xff;
                cnt -= 8;
            }
        }
        for(int i = 0;i<tot;i++)
            in[i] = base[i];
        in[tot] = 0;
        printf("Case #%d: %s\n",cas++,in);
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        cin>>n>>in;
        encode(n);
        decode(n);
        ///
        encode(n);
        decodeplus(n);
    }
    return 0;
}

手速模拟题

标签:二进制   acm-icpc   位运算   

原文地址:http://blog.csdn.net/gg_gogoing/article/details/45970263

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!