注意155行,ww=ww.mm[xx[k]] (错误)
Obj tt=ww.mm[xx[k]]; (正确)
ww=tt;
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include<vector>
int n,m;
string text;
struct Obj{ //Obj 既可以是str类型,也可以是字典类型
int type; // 1 str,2 dict
string str;
Obj()
{
}
Obj(string _str)
{
str=_str;
}
map<string,Obj> mm;
};
string work_str(int &k) //返回一个字符串
{
string name;
while(text[k]!='\"')
{
if(text[k]=='\\')
{
k++;
name+=text[k];
}else
{
name+=text[k];
}
k++;
}
k--;
return name;
}
Obj work_str_obj(int &k) //返回一个类型为字符串的Obj
{
Obj obj;
string name;
while(text[k]!='\"')
{
if(text[k]=='\\')
{
k++;
name+=text[k];
}else{
name+=text[k];
}
k++;
}
k--;
obj.str=name;
obj.type=1;
return obj;
}
Obj work_txt(int &k) //返回一个字典
{
Obj OObj;
OObj.type=2;
while(k<text.size() && text[k]!='{')
{
k++;
}
k++;
if(text[k]=='}') //注意审题,可能有{}空字典
{
}else{
while(k<text.size() && text[k]!='\"')
k++;
while(text[k]!='}')
{
if(text[k]=='\"')
{
k++;
string name=work_str(k);
k++;
while(text[k]!=':')
k++;
k++;
while(text[k]!='{' && text[k]!='\"')
k++;
if(text[k]=='\"')
{
k++;
OObj.mm[name]=work_str_obj(k);
k++;
k++; //去掉字符串后面的“引号
}else{
OObj.mm[name]=work_txt(k);
k++;
}
}else {
k++;
}
}
}
return OObj;
}
vector<string> get_vec(string tt) //将A.B.C中A,B,C放入vector中
{
vector<string> ss;
for(int i=0;i<tt.size();i++)
{
int j=i;
while(j<tt.size() && tt[j]!='.')
{
j++;
}
ss.push_back(tt.substr(i,j-i));
i=j;
}
return ss;
}
int main()
{
cin>>n>>m;
getchar();
string tt;
for(int i=0;i<n;i++)
{
getline(cin,tt);
text+=tt;
}
int k=0;
Obj o=work_txt(k);
for(int i=0;i<m;i++)
{
Obj ww=o;
getline(cin,tt);
vector<string> xx=get_vec(tt);
int yes=1;
for(int k=0;k<xx.size();k++)
{
if(ww.mm.find(xx[k])==ww.mm.end())
{
cout<<"NOTEXIST"<<endl;
yes=0;
break;
}
Obj tt=ww.mm[xx[k]]; //注意!!!!
ww=tt;
}
if(yes)
{
if(ww.type==1)
{
cout<<"STRING "<<ww.str<<endl;
}else cout<<"OBJECT"<<endl;
}
}
return 0;
}