forked from rflin/ccf-csp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201709-3 JSON查询 ccf .cpp
110 lines (110 loc) · 2.25 KB
/
201709-3 JSON查询 ccf .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
bool isroot(string &jsonstr,size_t pos)
{
int pcnt=0;
for(size_t i=0;i<pos;++i)
{
if(jsonstr[i]=='{') ++pcnt;
if(jsonstr[i]=='}') --pcnt;
}
return pcnt==0;
}
vector<string> split(string str)
{
size_t pos=str.find(".");
vector<string> ret;
while(pos!=string::npos)
{
ret.push_back(str.substr(0,pos));
str=str.substr(pos+1);
pos=str.find(".");
}
ret.push_back(str);
return ret;
}
string subjsonstr(string jsonstr,int lpos)
{
int pcnt=1;
size_t rpos=lpos;
while(pcnt)
{
if(jsonstr[rpos]=='{')
pcnt++;
else if(jsonstr[rpos]=='}')
pcnt--;
++rpos;
}
return jsonstr.substr(lpos,rpos-lpos-1)+",";
}
void search(vector<string> &ret,string jsonstr)
{
size_t i=0;
while(i<ret.size()-1)
{
size_t pos=jsonstr.find(ret[i]+":{");
if(pos==string::npos||!isroot(jsonstr,pos))
{
cout<<"NOTEXIST\n";
return;
}
jsonstr=subjsonstr(jsonstr,pos+ret[i].size()+2);
++i;
}
size_t pos=jsonstr.find(ret.back()+":");
if(pos==string::npos||!isroot(jsonstr,pos))
{
cout<<"NOTEXIST\n";
}
else
{
i=pos+ret.back().size()+1;
if(jsonstr[i]=='{')
cout<<"OBJECT\n";
else
{
size_t dp=jsonstr.find(",",i);
if(dp==string::npos)
{
cout<<"NOTEXIST\n";
return;
}
string x;
while(i<dp)
x+=jsonstr[i++];
cout<<"STRING "<<x<<endl;
}
}
}
int main()
{
//freopen("1.txt","r",stdin);
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 str;
cin>>str;
vector<string> ret=split(str);
search(ret,jsonstr);
}
return 0;
}