We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Gemstones
You are viewing a single comment's thread. Return to all 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.