#include #include #include #include #include const std::string string_of_valid_symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?@*"; const std::set set_of_valid_symbols(string_of_valid_symbols.begin(), string_of_valid_symbols.end()); bool validate_int(std::string const &str, unsigned from, unsigned until) { // Empty string is not an int if (from == until) return false; // A string longer than -1000000 is not an int if (from + 8 < until) return false; // Zero is an int if (from + 1 == until && str[from] == '0') return true; bool is_prefix = true; int modulo = 0; for (unsigned i = from; i < until; ++i) { char c = str[i]; if (c == '-') { if (i != from) return false; is_prefix = true; } else if (c == '0') { if (is_prefix) return false; modulo *= 10; } else if (c >= '1' && c <= '9') { is_prefix = false; modulo = 10 * modulo + c - '0'; } else { exit(1); } } return modulo <= 1000000; } int main() { // First line validation std::string s; getline(std::cin, s); if (s.length() == 0 || s.length() > string_of_valid_symbols.length()) { std::cerr << "First line is empty or too long" << std::endl; return 43; } for (char c : s) { if (set_of_valid_symbols.count(c) == 0) { std::cerr << "First line: invalid symbol found (" << c << ")" << std::endl; return 43; } } if (std::unique(s.begin(), s.end()) != s.end()) { std::cerr << "First line: duplicates found" << std::endl; return 43; } for (unsigned row = 0; row < s.length(); ++row) { std::string line; getline(std::cin, line); unsigned last_ws = -1; unsigned number_count = 0; for (unsigned i = 0; i < line.length(); ++i) { char c = line[i]; if (c == ' ') { if (i == 0) { std::cerr << "Matrix line " << row + 1 << " starts with a whitespace" << std::endl; return 43; } if (last_ws == i - 1) { std::cerr << "Matrix line " << row + 1 << ": Multiple whitespace in a row" << std::endl; return 43; } if (!validate_int(line, last_ws + 1, i)) { std::cerr << "Matrix line " << row + 1 << ": Not a valid int before position " << i << std::endl; return 43; } ++number_count; last_ws = i; } else if (c == '-') { if (last_ws != i - 1) { std::cerr << "Matrix line " << row + 1 << ": A minus comes not after whitespace/line start" << std::endl; return 43; } } else if (c < '0' || c > '9') { std::cerr << "Matrix line " << row + 1 << ": Illegal symbol (" << c << ")" << std::endl; return 43; } } if (last_ws == line.length() - 1) { std::cerr << "Matrix line " << row + 1 << " ends with a whitespace" << std::endl; return 43; } if (!validate_int(line, last_ws + 1, line.length())) { std::cerr << "Matrix line " << row + 1 << ": Not a valid int at the end" << std::endl; return 43; } ++number_count; if (number_count != s.length() - row) { std::cerr << "Matrix line " << row + 1 << ": " << s.length() - row << " numbers expected, " << number_count << " found" << std::endl; } } std::string trash; if (std::cin >> trash) { std::cerr << "Extra data at the end of input (" << trash << ")" << std::endl; return 43; } return 42; }