• + 1 comment
    #include <bits/stdc++.h>
    
    using namespace std;
    
    string ltrim(const string &);
    string rtrim(const string &);
    
    /*
     * Complete the 'winningLotteryTicket' function below.
     *
     * The function is expected to return a LONG_INTEGER.
     * The function accepts STRING_ARRAY tickets as parameter.
     */
    
    long winningLotteryTicket(vector<string> tickets) {
        // A map to store the frequency of each bitmask
        unordered_map<int, long> bitmask_count;
        
        // Process each ticket and compute its bitmask
        for (const string& ticket : tickets) {
            int bitmask = 0;
            
            // Mark the digits that appear in the ticket
            for (char c : ticket) {
                bitmask |= (1 << (c - '0'));
            }
            
            // Increment the count for this bitmask
            bitmask_count[bitmask]++;
        }
        
        long result = 0;
        
        // Now check pairs of bitmasks
        for (auto it1 = bitmask_count.begin(); it1 != bitmask_count.end(); ++it1) {
            for (auto it2 = 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 same
        for (auto it1 = bitmask_count.begin(); it1 != bitmask_count.end(); ++it1) {
            if ((it1->first | it1->first) == 1023) {
                result += (it1->second * (it1->second - 1)) / 2;
            }
        }
    
        return result;
    }
    
    int main()
    {
        ofstream fout(getenv("OUTPUT_PATH"));
    
        string n_temp;
        getline(cin, n_temp);
    
        int n = stoi(ltrim(rtrim(n_temp)));
    
        vector<string> tickets(n);
    
        for (int i = 0; i < n; i++) {
            string tickets_item;
            getline(cin, tickets_item);
    
            tickets[i] = tickets_item;
        }
    
        long result = winningLotteryTicket(tickets);
    
        fout << result << "\n";
    
        fout.close();
    
        return 0;
    }
    
    string ltrim(const string &str) {
        string s(str);
    
        s.erase(
            s.begin(),
            find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
        );
    
        return s;
    }
    
    string rtrim(const string &str) {
        string s(str);
    
        s.erase(
            find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
            s.end()
        );
    
        return s;
    }