// leetcode.h - runs a LeetCode solution (class Solution { ... }) locally. // LeetCode problems' main.cpp includes this under #ifdef LOCAL only: LeetCode // already defines ListNode/TreeNode itself, so main.cpp stays submittable. // The per-problem driver.h that 'usaco new ' writes uses the // helpers below to read LeetCode's testcase format (one JSON-ish argument per // line) and print answers the way LeetCode's "Output" box shows them. #pragma once #include struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {} }; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} }; namespace lc { using namespace std; [[noreturn]] inline void fail(const string &msg) { cerr << "leetcode.h: " << msg << '\n'; exit(1); } // ----- PARSING ----- // A parsed value. Numbers keep their source text so ints, longs and doubles // all convert exactly. struct Val { enum Kind { NUL, BOOL, NUM, STR, ARR } kind = NUL; string s; // NUM: as written, STR: decoded text, BOOL: "true"/"false" vector arr; }; struct Parser { const string &t; size_t i = 0; void ws() { while (i < t.size() && isspace((unsigned char)t[i])) i++; } void expect(char c) { ws(); if (i >= t.size() || t[i] != c) fail(string("expected '") + c + "' at column " + to_string(i + 1) + " of: " + t); i++; } bool word(const char *w) { size_t n = strlen(w); if (t.compare(i, n, w) != 0) return false; i += n; return true; } Val value() { ws(); if (i >= t.size()) fail("unexpected end of line: " + t); Val v; if (t[i] == '[') { v.kind = Val::ARR; i++; ws(); if (i < t.size() && t[i] == ']') return i++, v; for (;;) { v.arr.push_back(value()); ws(); if (i < t.size() && t[i] == ',') { i++; continue; } expect(']'); return v; } } if (t[i] == '"') { v.kind = Val::STR; for (i++; i < t.size() && t[i] != '"'; i++) { if (t[i] != '\\' || i + 1 >= t.size()) { v.s += t[i]; continue; } switch (t[++i]) { case 'n': v.s += '\n'; break; case 't': v.s += '\t'; break; case 'u': v.s += (char)stoi(t.substr(i + 1, 4), nullptr, 16), i += 4; break; default: v.s += t[i]; } } expect('"'); return v; } if (word("null")) return v; for (const char *b : {"true", "false"}) if (word(b)) { v.kind = Val::BOOL; v.s = b; return v; } size_t j = i; while (j < t.size() && (isdigit((unsigned char)t[j]) || strchr("+-.eE", t[j]))) j++; if (j == i) fail("can't read a value at column " + to_string(i + 1) + " of: " + t); v.kind = Val::NUM; v.s = t.substr(i, j - i); i = j; return v; } }; inline Val parse_val(const string &line) { Parser p{line}; Val v = p.value(); p.ws(); if (p.i != line.size()) fail("unexpected text after the value: " + line); return v; } template void from(const Val &v, T &x); template T as(const Val &v) { T x{}; from(v, x); return x; } template T parse(const string &line) { return as(parse_val(line)); } inline void need(const Val &v, Val::Kind k, const char *what) { if (v.kind != k) fail(string("expected ") + what + ", got " + (v.kind == Val::NUL ? "null" : v.kind == Val::ARR ? "an array" : v.s)); } template void from(const Val &v, T &x) { if constexpr (is_same_v) { need(v, Val::BOOL, "true/false"); x = v.s == "true"; } else if constexpr (is_same_v) { need(v, Val::STR, "a character"); x = v.s.empty() ? '\0' : v.s[0]; } else if constexpr (is_integral_v) { need(v, Val::NUM, "an integer"); x = (T)stoll(v.s); } else if constexpr (is_floating_point_v) { need(v, Val::NUM, "a number"); x = (T)stod(v.s); } else if constexpr (is_same_v) { need(v, Val::STR, "a string"); x = v.s; } else if constexpr (is_same_v) { need(v, Val::ARR, "a list"); ListNode head, *tail = &head; for (auto &e : v.arr) tail = tail->next = new ListNode(as(e)); x = head.next; } else if constexpr (is_same_v) { // level order with nulls for missing children: [1,null,2,3] need(v, Val::ARR, "a tree"); auto &a = v.arr; x = nullptr; if (a.empty() || a[0].kind == Val::NUL) return; x = new TreeNode(as(a[0])); queue q; q.push(x); for (size_t i = 1; !q.empty() && i < a.size();) { TreeNode *n = q.front(); q.pop(); for (TreeNode **child : {&n->left, &n->right}) { if (i < a.size() && a[i].kind != Val::NUL) q.push(*child = new TreeNode(as(a[i]))); i++; } } } else { // vector<...> need(v, Val::ARR, "an array"); x.clear(); for (auto &e : v.arr) x.push_back(as(e)); } } // One test case = k non-blank lines. False at the end of the input. inline bool read_case(vector &in, size_t k) { in.clear(); string line; while (in.size() < k && getline(cin, line)) if (line.find_first_not_of(" \t\r") != string::npos) in.push_back(line); if (!in.empty() && in.size() < k) cerr << "leetcode.h: input ended mid-case (got " << in.size() << " of " << k << " lines)\n"; return in.size() == k; } // ----- PRINTING ----- inline string list(const vector &items) { string r = "["; for (size_t i = 0; i < items.size(); i++) r += (i ? "," : "") + items[i]; return r + "]"; } inline string quote(const string &s) { string r = "\""; for (char c : s) { if (c == '"' || c == '\\') r += '\\'; r += c == '\n' ? string("\\n") : string(1, c); } return r + "\""; } template string show(const T &x) { if constexpr (is_same_v) { return x ? "true" : "false"; } else if constexpr (is_same_v) { return quote(string(1, x)); } else if constexpr (is_integral_v) { return to_string(x); } else if constexpr (is_floating_point_v) { char buf[64]; // LeetCode shows doubles with 5 decimals snprintf(buf, sizeof buf, "%.5f", (double)x); return buf; } else if constexpr (is_same_v) { return quote(x); } else if constexpr (is_same_v) { vector out; for (ListNode *n = x; n; n = n->next) if (out.size() > 1000000) fail("list has over a million nodes -- is there a cycle?"); else out.push_back(to_string(n->val)); return list(out); } else if constexpr (is_same_v) { vector out; queue q; q.push(x); while (!q.empty()) { TreeNode *n = q.front(); q.pop(); out.push_back(n ? to_string(n->val) : "null"); if (n) q.push(n->left), q.push(n->right); } while (!out.empty() && out.back() == "null") out.pop_back(); return list(out); } else { // vector<...> using E = typename T::value_type; vector out; for (auto &&e : x) out.push_back(show(static_cast(e))); // static_cast: vector return list(out); } } template T prefix(const T &v, size_t k) { return T(v.begin(), v.begin() + min(k, v.size())); } } // namespace lc