思路
先对输入的json文件进行预处理,去掉(不包含在引号里的)空格、引号、逗号、冒号等字符,将其以空格代替,然后根据利用stringstream处理即可(虽然stringstream速度很慢,但是刷CCF足够)。
#include <bits/stdc++.h>
using namespace std;
int n, m, in;
string s, json = "哈哈哈哈 ", tmp;
unordered_map<string, string> hp;
vector<string> path;
int main() {
scanf("%d %d\n", &n, &m);
while(n--) getline(cin, tmp, '\n'), s += tmp;
for(int i = 0; i < s.size(); i++) {
if('\"' == s[i]) in = !in, json += string(1, ' ');
else if(!in && ('{' == s[i] || '}' == s[i])) json += string(1, s[i]) + string(1, ' ');
else if(!in && (':' == s[i] || ',' == s[i])) json += string(1, ' ');
else if(!in && ' ' == s[i]);
else if(in && '\\' == s[i] && i + 1 < s.size()) json += string(1, s[i + 1]), i++;
else json += string(1, s[i]);
}
stringstream out(json);
while(out >> s) {
if(s == "}") path.pop_back();
else {
string key;
out >> tmp;
for(int i = 1; i < path.size(); i++) key += string(i > 1, '.') + path[i];
key = key + string(path.size() > 1, '.') + s;
if(tmp == "{") hp[key] = "OBJECT", path.push_back(s);
else hp[key] = "STRING " + tmp;
}
}
while(m-- && cin >> s) cout << (hp.find(s) == hp.end() ? "NOTEXIST" : hp[s]) << endl;
return 0;
}