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.
#include<bits/stdc++.h>usingnamespacestd;stringltrim(conststring&);stringrtrim(conststring&);/* * Complete the 'winningLotteryTicket' function below. * * The function is expected to return a LONG_INTEGER. * The function accepts STRING_ARRAY tickets as parameter. */longwinningLotteryTicket(vector<string>tickets){// A map to store the frequency of each bitmaskunordered_map<int,long>bitmask_count;// Process each ticket and compute its bitmaskfor(conststring&ticket:tickets){intbitmask=0;// Mark the digits that appear in the ticketfor(charc:ticket){bitmask|=(1<<(c-'0'));}// Increment the count for this bitmaskbitmask_count[bitmask]++;}longresult=0;// Now check pairs of bitmasksfor(autoit1=bitmask_count.begin();it1!=bitmask_count.end();++it1){for(autoit2=next(it1);it2!=bitmask_count.end();++it2){if((it1->first|it2->first)==1023){result+=it1->second*it2->second;}}}// Check the cases where both bitmasks are the samefor(autoit1=bitmask_count.begin();it1!=bitmask_count.end();++it1){if((it1->first|it1->first)==1023){result+=(it1->second*(it1->second-1))/2;}}returnresult;}intmain(){ofstreamfout(getenv("OUTPUT_PATH"));stringn_temp;getline(cin,n_temp);intn=stoi(ltrim(rtrim(n_temp)));vector<string>tickets(n);for(inti=0;i<n;i++){stringtickets_item;getline(cin,tickets_item);tickets[i]=tickets_item;}longresult=winningLotteryTicket(tickets);fout<<result<<"\n";fout.close();return0;}stringltrim(conststring&str){strings(str);s.erase(s.begin(),find_if(s.begin(),s.end(),not1(ptr_fun<int,int>(isspace))));returns;}stringrtrim(conststring&str){strings(str);s.erase(find_if(s.rbegin(),s.rend(),not1(ptr_fun<int,int>(isspace))).base(),s.end());returns;}
Winning Lottery Ticket
You are viewing a single comment's thread. Return to all comments →