• + 2 comments

    my code works fine, but want to replace the last for-loop in main, that checks the number of bits on in uint32 with some more optimised code?

    basically i used uint32 to store the 26 alphabets. Parse each string and turn ON the corresponding bit in uint32 and return the variable. Which is bitwise-AND with master uint32 variable.

    unsigned int processing (const std::string& str) {
    // find out and return the number of 'chars' that 
    unsigned int tmp = 0x00000000;
    auto endpt = str.end()
    for (auto iter = str.begin(); iter != endpt; iter++) {
        int val = 'z' - *iter;
        //cout << "cur val: " << *iter <<" : " << val << endl;
        tmp |= (0x00000001 << val);
    }
    return tmp;
    }
    
    int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int t;
    cin >> t;
    
    char master_list[CNT];
    unsigned int can = 0xEFFFFFFF;
    for (int i=0; i < CNT; i++) master_list[i] = 'x';
    int ret = 0;
    while (t--) {
        std::string arr;
        cin >> arr;
        //cout << "input: "<< arr << endl;
        can &= processing(arr);
    }
    //cout << "result: " << hex << can << endl;
    // finding number of bits ON in uint32
    unsigned int a = 0x00000001;
    int count = 0;
    for (int i =0; i<32; i++) {
        if ((can & a) != 0) count++;
        a = a << 1;
    }
    cout << count << endl;
    return 0;
    }