a348困難非遞迴 JSON 邊界掃描與原文擷取
TOI 2020/10 潛力組:JSON
不正規化 JSON,依存取路徑輸出原字串對應片段。
解題思路
先掃描原文,為每個值的開頭記錄 end[start],代表它結束位置的下一格。遇到雙引號就完整跳過字串,字串內的括號、逗號都不視為結構;題目不含跳脫字元,所以找到下一個雙引號即可。左右中括號與大括號用堆疊配對,數字和布林值讀到逗號、結尾括號或空白停止。這樣即使巢狀上萬層也不需要函式遞迴。
從 obj 指向的根值開始。陣列查詢就用 end 跳過前面幾個完整元素;物件查詢則讀鍵字串、跳過冒號,若鍵不符便以 end 跳過整個值。找到後把目前範圍縮到該值,繼續下一個查詢。掃過而排除的兄弟區塊不會再進入,沿途被選中的容器只處理它的開頭,因此全部跳躍與比較總長為線性。最後直接輸出 s[start:end[start]],保留引號、內部空白與物件鍵順序;只有 obj 時輸出整行原文。
來源沒有定義不存在的鍵、越界索引或對純量繼續存取的輸出。本程式對有效存取完成求值;遇到無效存取會報錯,沒有自行指定 null 或其他可能被誤認為題目規定的結果。
Step-by-step animation
範例 {"Kirito":[{"Busna":"OAO"},"startburst"]}
步驟 1 / 3
根物件
先配對所有容器邊界,obj 目前指向整份 JSON。
{"Kirito":[{"Busna":"OAO"},"startburst"]}
- 剩餘路徑
- ["Kirito"][0]
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
#include <stdexcept>
using namespace std;
int main() {
string s, o;
getline(cin, s);
getline(cin, o);
int n = s.size();
vector<int> end(n, -1), stack;
auto white = [](char c) { return isspace((unsigned char)c); };
auto skip = [&](int p) {
while (p < n && white(s[p]))
++p;
return p;
};
for (int i = 0; i < n;) {
if (white(s[i]) || s[i] == ',' || s[i] == ':') {
++i;
continue;
}
if (s[i] == '"') {
int j = i + 1;
while (j < n && s[j] != '"')
++j;
end[i] = j + 1;
i = j + 1;
} else if (s[i] == '[' || s[i] == '{') {
stack.push_back(i++);
} else if (s[i] == ']' || s[i] == '}') {
end[stack.back()] = i + 1;
stack.pop_back();
++i;
} else {
int j = i;
while (j < n && !white(s[j]) && s[j] != ',' && s[j] != ']' && s[j] != '}')
++j;
end[i] = j;
i = j;
}
}
int current = skip(0), p = 3;
bool accessed = false;
while (p < (int)o.size()) {
if (white(o[p])) {
++p;
continue;
}
if (o[p++] != '[')
throw runtime_error("Invalid access");
accessed = true;
while (p < (int)o.size() && white(o[p]))
++p;
if (o[p] == '"') {
int begin = ++p;
while (p < (int)o.size() && o[p] != '"')
++p;
string key = o.substr(begin, p - begin);
++p;
if (s[current] != '{')
throw runtime_error("Invalid object access");
int j = skip(current + 1);
bool found = false;
while (j < n && s[j] != '}') {
int keyEnd = end[j];
string actual = s.substr(j + 1, keyEnd - j - 2);
j = skip(keyEnd);
if (s[j++] != ':')
throw runtime_error("Invalid JSON");
j = skip(j);
if (actual == key) {
current = j;
found = true;
break;
}
j = skip(end[j]);
if (s[j] == ',')
j = skip(j + 1);
else
break;
}
if (!found)
throw runtime_error("Unspecified missing key");
} else {
long long index = 0;
bool digits = false;
while (p < (int)o.size() && isdigit((unsigned char)o[p])) {
digits = true;
index = min(65526LL, index * 10 + o[p++] - '0');
}
if (!digits || s[current] != '[')
throw runtime_error("Invalid array access");
int j = skip(current + 1);
while (index--) {
if (j >= n || s[j] == ']')
throw runtime_error("Unspecified array index");
j = skip(end[j]);
if (s[j] != ',')
throw runtime_error("Unspecified array index");
j = skip(j + 1);
}
if (j >= n || s[j] == ']')
throw runtime_error("Unspecified array index");
current = j;
}
while (p < (int)o.size() && white(o[p]))
++p;
if (p == (int)o.size() || o[p++] != ']')
throw runtime_error("Invalid access");
}
cout << (accessed ? s.substr(current, end[current] - current) : s) << '\n';
}先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。