csp201709-3 json查询
2018-02-07 13:41
387 查看
转载自L_Aster
#include<bits/stdc++.h> using namespace std; bool isroot(string & str,size_t pos) { int cnt=0; for(size_t i=0;i<pos;i++) { if(str[i]=='{')cnt++; if(str[i]=='}')cnt--; } return cnt==0; } vector<string> split(string str) { vector<string> vs; size_t f=str.find("."); while(f!=string::npos) { string tmp=str.substr(0,f); str=str.substr(f+1); vs.push_back(tmp); f=str.find("."); } vs.push_back(str); return vs; } string subjsonstr(string str,int lpos) { int pcnt=1; size_t rpos=lpos; while(pcnt) { if(str[rpos]=='{')pcnt++; if(str[rpos]=='}')pcnt--; rpos++; } return str.substr(lpos,rpos-lpos-1)+","; } void search(vector<string>& vs,string jsonstr) { size_t i=0; while(i<vs.size()-1) { size_t pos=jsonstr.find(vs[i]+":{"); if(pos==string::npos||!isroot(jsonstr,pos)) { cout<<"NOTEXIST\n";return; } jsonstr=subjsonstr(jsonstr,pos+vs[i].size()+2); i++; } size_t pos=jsonstr.find(vs.back()+":"); if(pos==string::npos||!isroot(jsonstr,pos)) { cout<<"NOTEXIST\n"; } else { i=pos+vs.back().size()+1; if(jsonstr[i]=='{') cout<<"OBJECT\n"; else { size_t dp=jsonstr.find(",",i) 9833 ; if(dp==string::npos) { cout<<"NOTEXIST\n";return; } string x; while(i<dp) x+=jsonstr[i++]; cout<<"STRING "<<x<<endl; } } } int main() { string jsonstr; int n,m; cin>>n>>m; cin.get(); cin.get(); for(int i=0;i<n;i++) { char ch; while((ch=cin.get())!='\n') { if(ch==' '||ch=='"')continue; if(ch=='\\') { jsonstr+=cin.get();continue; } jsonstr+=ch; } } jsonstr[jsonstr.length()-1]=','; while(m--) { string s; cin>>s; vector<string> vs=split(s); search(vs,jsonstr); } }
相关文章推荐
- CCF CSP 201709-3 JSON查询
- CCF CSP真题——201709-3 json查询
- CCF CSP 201709-3 JSON查询
- CCF 201709-3 JSON查询 (100分)
- CCF-CSP JSON串查询
- 201709-3 JSON查询 ccf
- ccf csp JSON查询
- CCF 201709-3 Json查询 Java
- 201709-3 JSON查询
- CCF 201709-3 JSON查询(Java)
- ccf 201709-3 Json查询
- CCF 201709-3 JSON查询
- jQuery学习笔记--ajax查询,jQuery解析返回的json数据 详解
- CCF CSP真题——201709-2 公共钥匙盒
- CCF JSON查询
- SQL Server中将查询结果转换为Json格式脚本分享
- postgresql json查询出来的数据多出来一个双引号的问题
- CCF CSP 201709-2:公共钥匙盒
- 查询json数据结构的8种方式
- Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法