This is problem seems tricky at first, but the constrained input size (up to 10 questions per test) allows us to simply use brute force to find a solution.
We can generate all possible answer keys and find which one renders the optimal score.
Keys are generated by counting up from 0 to 2^k, where k is the number of questions on the test.
Using the current count as a bit mask, we match its set bits against T's and unset bits against F's.
_35const int kMaxStudents = 1000;
_35const int kMaxQuestions = 10;
_35const int kMaxKeys = 1 << kMaxQuestions;
_35int withKey[kMaxStudents][kMaxKeys];
_35int worstScore[kMaxKeys];
_35 const int kPossibleKeys = 1 << k;
_35 for (int i = 0; i < kPossibleKeys; ++i) {
_35 for (int i = 0; i < n; ++i) {
_35 for (int key = 0; key < kPossibleKeys; ++key) {
_35 withKey[i][key] = 0; // initialize
_35 for (int question = 0; question < k; ++question) {
_35 bool trueOnKey = key & 1 << question;
_35 if ((s[question] == 'T' && trueOnKey) || (s[question] == 'F' && !trueOnKey)) {
_35 worstScore[key] = min(worstScore[key], withKey[i][key]);
_35 cout << *max_element(begin(worstScore), end(worstScore)) << '\n';
If you found this solution helpful, consider leaving a star!