I want to understand the issue with my code

Problem Info

Bovine Genomics USACO


I am not able to figure out what is wrong with the approach I am trying explained below.

What I’ve Tried

I have used two sets to differentiate between spotted cows and plain cows. If a string in spotted set is also present in plain set then that is set is invalid. If the set is valid then adding that set to valid sets.

My Work

int n, m;
    cin >> n >> m;
    vector<string> spotted(n), plain(n);
    for (string &st: spotted) cin >> st;
    for (string &st: plain) cin >> st;

    int valid_sets = 0;
    for (int i = 0; i < m; i++) {
        for (int j = i+1; j < m; j++) {
            for (int k = j+1; k < m; k++) {
                set<string> spot_set, plain_set;
                for (int l = 0; l < n; l++) {
                    string a = "" + spotted[l][i] + spotted[l][j] + spotted[l][k];
                    string b = "" + plain[l][i] + plain[l][j] + plain[l][k];
                bool ok = true;
                for (const string &str: spot_set) {
                    if (plain_set.count(str)) {
                        ok = false;
                if (ok) valid_sets++;
    cout << valid_sets << "\n";

For each set of (i, j, k) adding a string for each of the spotted and plain cows in spot_set and plain_set. If a string present in spot_set is also present is plain_set, then that set is invalid, otherwise adding that set to valid sets.