CCFCSP-201709-3 JSON查询 大模拟

原题链接

注意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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值