USACO 2017 Bronze US Open Bovine Genomics

Problem
For the sample test case, I get the correct answer -1- when I run it on my computer, but the USACO grader says that my output file says “0”. Here is my code for reference:

#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
int n, m;
string spotty[100], plain[100];
int main()
{
 ifstream cin("cownomics.in"); ofstream cout("cownomics.out");
 ios_base::sync_with_stdio(false);
 cin.tie(0);
 cin >> n >> m;
 bool possible = true;
 for (int i = 0; i < n; i++)
 {
   cin >> spotty[i];
 }
 for (int i = 0; i < n; i++)
 {
   cin >> plain[i];
 }
 int count = 0;
 //a spot is potential iff there is no overlap between the genomes there of plain and spotted cows
 //iterate over indices
   //possible = true
   //iterate over spotty
     //add spottycow[i] to the set
   //iterate over plain 
     //add plaincow[i] to the other set
   //iterate over set1
     //iterate over set2
       //if elements are the same, possible=false and break
   //if possible, increment count
 for (int i = 0; i < m; i++)
 {
   possible = true;
   set<char> spotty_genes, plain_genes;
   for (auto cow : spotty)
   {
     spotty_genes.insert(cow[i]);
   }
   for (auto cow : plain)
   {
     plain_genes.insert(cow[i]);
   }
   for (auto x : spotty_genes)
   {
     for (auto y : plain_genes)
     {
       if (x == y) {
        possible = false;
        break;
     }
     }
   }
   if (possible) count++;
 }
 cout << count;
}

you should change for (auto cow : spotty) and for (auto cow : plain) to regular for loops

Thanks, that worked! Why did I have to do that? When do for-each loops not work?

for(auto cow : spotty) loops through every 100 strings in the array (which most of them are not initialized for the sample case).

1 Like