1932. 歪比巴卜 -- bitmasks,implementation


 

#include 
using namespace std;
using LL = long long;

int a[1005];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%x", &a[i]);
    unsigned int a1 = 0b00000000;//异或
    unsigned int a21 = 0b11000000, a22 = 0b10000000;
    unsigned int a31 = 0b11100000, a32 = 0b10000000, a33 = 0b10000000;
    unsigned int b1 = 0b10000000;//
    unsigned int b21 = 0b11100000, b22 = 0b11000000;
    unsigned int b31 = 0b11110000, b32 = 0b11000000, b33 = 0b11000000;
    unsigned int c1 = 0b01111111;//
    unsigned int c21 = 0b00011111, c22 = 0b00111111;
    unsigned int c31 = 0b00001111, c32 = 0b00111111, c33 = 0b00111111;
    int cnt = 0;
    vector<int> v;
    for (int i = 1; i <= n;) {
        if (i + 1 <= n 
            && !((a[i] ^ a21) & b21) \
            && !((a[i + 1] ^ a22) & b22))
        {
            int res =  ((a[i] & c21) << 6 )
                        + (a[i + 1] & c22);
            v.emplace_back(res);
            i += 2;
        }
        else if (i + 2 <= n
            && !((a[i] ^ a31) & b31)
            && !((a[i + 1] ^ a32) & b32)
            && !((a[i + 2] ^ a33) & b33)
            )
        {
            int res =  ((a[i] & c31) << 12)
                        + ((a[i + 1] & c32) << 6 )
                        + (a[i + 2] & c33);
            v.emplace_back(res);
            i += 3;
        }
        else {
            v.emplace_back(a[i] & c1);
            i++;
        }
        cnt += 1;
    }
    printf("%d\n", cnt);
    for(auto i : v) {
        printf("%d ", i);
        
    }
    return 0;
}