給出一個 JSON 格式描述的數據,以及若干查詢,編程返回這些查詢的結果。c++
接下來 m 行,每行描述一個查詢。給出要查詢的屬性名,要求返回對應屬性的內容。須要支持多層查詢,各層的屬性名之間用小數點 . 鏈接。保證查詢的格式都是合法的。編程
若是查詢結果是一個對象,則輸出 OBJECT,不須要輸出對象的內容。
若是查詢結果不存在,則輸出 NOTEXIST。app
10 5
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "2ndStreet",
"city": "NewYork",
"state": "NY"
},
"esc\\aped": ""hello""
}
firstName
address
address.city
address.postal
esc\aped函數
STRING John
OBJECT
STRING NewYork
NOTEXIST
STRING "hello"post
50%的評測用例輸入的對象只有 1 層結構,80%的評測用例輸入的對象結構層數不超過 2 層。舉例來講,{"a": "b"} 是一層結構的對象,{"a": {"b": "c"}} 是二層結構的對象,以此類推。spa
#include <bits/stdc++.h> using namespace std; int n,m,num,i; string tot,x; string ql[100]; string str1,str2; char str[10000]; map<string,string>mp; void input() { cin>>n>>m; getchar(); tot="";num=0; for(int i=0;i<n;i++) { getline(cin,x); tot.append(x); } for(int i=0;i<m;i++){ getline(cin,ql[i]); } int i=0; while(tot[i]==' ') i++; for(;i<tot.size();i++){ while(tot[i]==' ') i++; str[num++]=tot[i]; } } string find() { i++; string tar; while(str[i]!='\"'){ if(str[i]!='\\'){ tar+=str[i]; i++; }else { tar+=str[i+1]; i+=2; } } i++; //cout<<tar<<endl; return tar; } void solve(string head) { for(;i<num;i++) { if(str[i]=='\"') { str1 = find(); if(head!="") str1 = head+'.'+str1; i++; if(str[i]=='\"'){ str2 = find(); //cout<<str1<<" "<<str2<<endl; mp[str1]=str2; }else { mp[str1]="####"; solve(str1); } } if(str[i]=='}'){i++;return;} } } int main() { i=0; input(); //cout<<str<<endl; solve(""); for(int i=0;i<m;i++) { //cout<<mp[ql[i]]<<endl; if(!mp.count(ql[i])){ cout<<"NOTEXIST"<<endl; //cout<<"STRING "<<mp[ql[i]]<<endl; }else { if(mp[ql[i]]=="####")cout<<"OBJECT"<<endl; else cout<<"STRING "<<mp[ql[i]]<<endl; } } return 0; }