#include #include #include #include #include #include #include "validate.h" using namespace std; void right_trim(string &s) { s.erase(find_if(rbegin(s), rend(s), [](char c) { return !isspace(c); }).base(), end(s)); } int main(int argc, char **argv) { init_io(argc,argv); int n, W; judge_in >> n >> W; vector words(n); for (string &word: words) judge_in >> word; int best; judge_ans >> best; int r, c; if (!(author_out >> r >> c)) { wrong_answer("Wrong answer: expected two integers, the number of rows and columns.\n"); } if (r < 1 || r > n) { wrong_answer("Wrong answer: invalid row count: %d.\n", r); } if (c < 1 || c > W) { wrong_answer("Wrong answer: invalid column count: %d.\n", c); } vector w(c); for (int i = 0; i < c; i++) { if (!(author_out >> w[i])) { wrong_answer("Wrong answer: expected %d integers, the column widths.\n", c); } if (w[i] < 1 || w[i] > W) { wrong_answer("Wrong answer: invalid column width: %d.\n", w[i]); } } int wsum = accumulate(begin(w), end(w), c-1); if (wsum > W) { wrong_answer("Wrong answer: total width of columns is too large " "(should be at most %d, but is %d).\n", W, wsum); } // Strip end of line before table: string junk; getline(author_out, junk); vector grid(r); for (int i = 0; i < r; i++) { getline(author_out, grid[i]); right_trim(grid[i]); int cw = grid[i].size(); if (cw > W) { wrong_answer("Wrong answer: %d-th line of table too wide: %d.\n", i+1, cw); } // Pad the table to width W: grid[i] += string(W-cw,' '); } // Look for the input words one by one, skipping to the next column whenever the // lower end of the grid is reached or an empty space is encountered. // After checking, each word is overwritten by spaces. int i = 0, j = 0, s = 0; // row index, column index, start position of current column for (string word: words) { if (i == r || grid[i][s] == ' ') { i = 0, s += w[j]+1, j++; } if (j == c) { wrong_answer("Wrong answer: did not place all the words within %d columns.\n", c); } if (int(word.size()) > w[j]) { wrong_answer("Wrong answer: word %s too wide for column %d.\n", word.c_str(), j+1); } int y = s; for (char c: word) { if (y >= W) { wrong_answer("Wrong answer: word %s too wide for column %d.\n", word.c_str(), j+1); } if (grid[i][y] != c) { wrong_answer("Wrong answer: found unexpected character while checking word %s: '%c'.\n", word.c_str(), grid[i][y]); } grid[i][y++] = ' '; } i++; } // The grid should be completely empty now. for (string row: grid) for (char c: row) if (c != ' ') { wrong_answer("Wrong answer: found extraneous characters.\n"); } if (author_out >> junk) { wrong_answer("Wrong answer: found trailing output.\n"); } if (r > best) { wrong_answer("Wrong answer: formatting ok, but solution not optimal " "(using %d rows while %d is the minimum).\n", r, best); } else if (r < best) { judge_error("Judge error: author found better solution than judge!\n"); } accept(); }