Exam Manipulation

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.

exam.cpp
exam.py

_35
#include <iostream>
_35
#include <string>
_35
#include <algorithm>
_35
#include <numeric>
_35
using namespace std;
_35
const int kMaxStudents = 1000;
_35
const int kMaxQuestions = 10;
_35
const int kMaxKeys = 1 << kMaxQuestions;
_35
_35
int withKey[kMaxStudents][kMaxKeys];
_35
int worstScore[kMaxKeys];
_35
_35
int main() {
_35
int n,k;
_35
cin >> n >> k;
_35
const int kPossibleKeys = 1 << k;
_35
for (int i = 0; i < kPossibleKeys; ++i) {
_35
worstScore[i] = 11;
_35
}
_35
for (int i = 0; i < n; ++i) {
_35
string s;
_35
cin >> s;
_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
withKey[i][key]++;
_35
}
_35
}
_35
worstScore[key] = min(worstScore[key], withKey[i][key]);
_35
}
_35
}
_35
cout << *max_element(begin(worstScore), end(worstScore)) << '\n';
_35
}

If you found this solution helpful, consider leaving a star!