#include #include #include #include #include "validate.h" typedef std::vector< std::vector > matrix_t; int parse_output(std::string const &symbols, matrix_t const &d, std::istream &in, feedback_function feedback) { unsigned answer_size; if (!(in >> answer_size)) { feedback("Cound not read the answer size"); } if (answer_size > symbols.length()) { feedback("Declared answer length is longer than the problem size"); } std::string answer; if (answer_size > 0 && !(in >> answer)) { feedback("Cound not read the answer"); } if (answer_size != answer.length()) { feedback("Answer lengths do not match (declared %u, actual %u)", answer_size, answer.length()); } if (std::unique(answer.begin(), answer.end()) != answer.end()) { feedback("Answer contains duplicates"); } std::vector extracted_indices(answer_size); for (unsigned i = 0; i < answer_size; ++i) { unsigned index = symbols.find(answer[i]); if (index == std::string::npos) { feedback("Illegal symbol at position %u", i); } extracted_indices[i] = index; } int cost = 0; for (unsigned i = 0; i < answer_size; ++i) { for (unsigned j = i; j < answer_size; ++j) { int ii = extracted_indices[i]; int jj = extracted_indices[j]; cost += d[std::min(ii, jj)][std::max(ii, jj)]; } } return cost; } int main(int argc, char **argv) { init_io(argc, argv); // Read problem input std::string symbols; if (!(judge_in >> symbols)) { judge_error("Cannot read problem input (symbol string)"); } unsigned n = symbols.length(); std::vector< std::vector > d(n, std::vector(n, 0)); for (unsigned r = 0; r < n; ++r) { for (unsigned c = r; c < n; ++c) { if (!(judge_in >> d[r][c])) { judge_error("Cannot read problem input (d[%u][%u])", r + 1, c + 1); } } } // Read jury's answer int jury_strength = parse_output(symbols, d, judge_ans, judge_error); // Read contestant's answer int cont_strength = parse_output(symbols, d, author_out, wrong_answer); // If jury is worse, this is something really wrong if (jury_strength < cont_strength) { judge_error("Contestant (%d) is better than jury (%d)", cont_strength, jury_strength); } // If contestant is worse, alas if (jury_strength > cont_strength) { wrong_answer("Contestant (%d) is worse than jury (%d)", cont_strength, jury_strength); } // If not failed, accept accept(); }