zoukankan      html  css  js  c++  java
  • CCF-20170903--JSON查询

    首先,将全部的输入连接起来,存储到字符串str中,再将\全部替换为空格。
    再依次遍历str。
    对于:“A”:“B”这种情况,我们以:为分界点来分别存储key和value来进行存储
    对于:“A”:{
    “B”:“C”
    }的这种情况,我们如果在:号后面遇到的不是引号,那就将value设置为OBJECT,进行存储,这里主要要注意前缀问题,我们使用一个vector来模拟栈
    来处理前缀,每次遇到{,就将key添加到vector中,每次约到},就将vector最后面的元素删除。
    最后,这里有几个坑:
    1.考虑\*,*\的情况
    2.三层甚至多层的情况
    3.“A”:{},这种value是空的键值对的情况。

    #include<bits/stdc++.h>
    using namespace std;
    void trim(string &s)
    {
        int index = 0;
        if( !s.empty())
        {
            while( (index = s.find(' ',index)) != string::npos)
            {
                s.erase(index,1);
            }
        }
    }
    void operator_find(string & s,string findStr,string replaceStr){//将字符串s中的findStr替换为replaceStr。
    	int size=0;
    	while(s.find(findStr)!=string::npos){
    		size=s.find(findStr)+findStr.length();
    		s.replace(s.find(findStr),replaceStr.length(),replaceStr);
    	} 
    }
    int main(){
    	int m,n;
    	cin>>m>>n;
    	cin.ignore();
    	string str="";
    	map<string,string> mmps;
    	vector<string> stks;
    	for(int i=0;i<m;i++){
    		string tempStr;
    		getline(cin,tempStr);
    		str+=tempStr;
    	}
    	operator_find(str,"\\","\ ");
    	trim(str);
    	//cout<<str<<endl;
    	int i=0;
    	string ObjStr="";//用来存放Obj的前缀
    	int flag=0;//用来区别key与value;
    	string key="",value="";
    	while(str[i]!=''){
    		key="",value="";
    		//cout<<"Str[i]:"<<str[i]<<endl;
    		while(str[i]!='}'&&str[i++]!='"');
    		while(str[i]!=':'&&str[i]!='}'){//提取key值 
    			key+=str[i];
    			i++;	
    		}
    		if(str[i]!='}')//用于处理出现}}}}类似的情况 
    			i=i+1;//跳过冒号
    		if(str[i]=='"'){//提取value值 
    			i++;
    			while(str[i]!=','&&str[i]!='}'){
    				value+=str[i];
    				i++;	
    			}
    		}else{//value不是字符串,而是对象的情况 
    			if(key!=""||value!=""){
    				string Qianzui="";
    				if(stks.size()!=0){
    					for(int ii=0;ii<stks.size();ii++){
    						if(ii==0)
    							Qianzui+=stks[ii];
    						else
    							Qianzui=Qianzui+"."+stks[ii];
    					}
    				}
    				key=key.substr(0,key.length()-1);
    				operator_find(key,"\""," ");
    				trim(key);
    				stks.push_back(key);
    				if(Qianzui!="")
    					key=Qianzui+"."+key;
    				mmps[key]="OBJECT";		
    			}
    		}
    		if(str[i]==','){//存储键值对 
    			if(stks.size()!=0){
    				string Qianzui="";
    				for(int ii=0;ii<stks.size();ii++){
    					if(ii==0)
    						Qianzui+=stks[ii];
    					else
    						Qianzui=Qianzui+"."+stks[ii];
    				}
    				key=Qianzui+"."+key;
    			}
    			key=key.substr(0,key.length()-1);
    			value=value.substr(0,value.length()-1);
    			operator_find(key,"\""," ");
    			operator_find(value,"\""," ");
    			trim(key);
    			trim(value);
    			mmps[key]="STRING "+value;
    			key="",value="";
    		}
    		if(str[i]=='}'){//vector退栈的情况 
    			if(key!=""||value!=""){
    				if(stks.size()!=0){
    					string Qianzui="";
    					for(int ii=0;ii<stks.size();ii++){
    						if(ii==0)
    							Qianzui+=stks[ii];
    						else
    							Qianzui=Qianzui+"."+stks[ii];
    					}
    					key=Qianzui+"."+key;
    				}
    				key=key.substr(0,key.length()-1);
    				value=value.substr(0,value.length()-1);
    				operator_find(key,"\""," ");
    				operator_find(value,"\""," ");
    				trim(key);
    				trim(value);
    				mmps[key]="STRING "+value;
    				key="",value="";
    				if(stks.size()!=0){
    					stks.erase(stks.end()-1);		
    					}
    				}else{
    					if(stks.size()!=0){
    						stks.erase(stks.end()-1);		
    					}
    				}
    		}
    		i++;
    	}
    	for(int i=0;i<n;i++){
    		string temps;
    		cin>>temps;
    		map<string,string>::iterator it;
    		it=mmps.find(temps);
    		if(it==mmps.end()){
    			cout<<"NOTEXIST"<<endl;
    		}else{
    			cout<<mmps[temps]<<endl;
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    go 错误处理策略
    git merge
    oracle
    使用PHPExcel导入数据库,date数据的问题
    PhpWord使用
    ThinkPHP
    Memcache
    没用过docker就out了
    TCP三次挥手四次协议
    数据分析
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/8516864.html
Copyright © 2011-2022 走看看