CCF(JSON查询:40分):字符串+模拟

时间:2019-09-03
本文章向大家介绍CCF(JSON查询:40分):字符串+模拟,主要包括CCF(JSON查询:40分):字符串+模拟使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

JSON查询

201709-3

纯字符串模拟,考的就是耐心和细心。可惜这两样我都缺。。。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int maxn=102;
int n,m;
string row[maxn];
map<string,string> attr;
int main(){
    cin>>n>>m;
    getchar();
    for(int i=0;i<n;i++){
        getline(cin,row[i]);
        string temp="";
        for(int j=0;j<row[i].length();j++){
            if(row[i][j]!=' '){
                temp+=row[i][j];
            }
        }
        row[i]=temp;
    }
    // for(int i=0;i<n;i++)
    //     cout<<row[i]<<endl;
    int times=0;
    string temp="";
    string pre;
    int cnt=0;
    for(int i=0;i<n;i++){
        bool flag=false;
        for(int j=0;j<row[i].length();j++){
            if(row[i][j]=='{'&&j==0){
                continue;
            }else if(row[i][j]=='}'&&j==row[i].length()-1){
                continue;
            }else if(row[i][j]=='\"'&&!flag&&(j==0||row[i][j-1]!='\\')){
                flag=true;
                cnt++;
            }else if(row[i][j]=='\"'&&flag&&row[i][j-1]!='\\'){
                flag=false;
                cnt++;
                if(times>0){//已经出现过冒号了
                    attr[pre]=temp;
                    times=0;
                    //cout<<pre<<" "<<temp<<endl;
                }else{//还没有出现过冒号
                    pre=temp;
                }
                //cout<<pre<<" "<<temp<<endl;
                temp="";
            }else if(row[i][j]==':'){//键和值
                if(cnt%2==0){
                    times++;
                    temp="";
                }else temp+=row[i][j];
            }else if(row[i][j]==','){
                if(cnt==4){
                    temp="";
                    cnt=0;
                }else temp+=row[i][j];
            }else{
                temp+=row[i][j];
            }
        }
    }
    for(int i=0;i<m;i++){
        string key;
        cin>>key;
        temp="";
        for(int j=0;j<key.length();j++){
            if(key[j]=='\\'){
                temp+='\\';
            }
            if(key[j]=='\"'){
                temp+='\\';
            }
            temp+=key[j];
        }
        //cout<<temp<<endl;
        if(!attr.count(temp)){
            cout<<"NOTEXIST"<<endl;
        }else{
            cout<<"STRING ";
            string now=attr[temp];
            bool flag=false;
            for(int j=0;j<now.length();j++){
                if(now[j]=='\"'&&now[j-1]=='\\')
                    flag=false;
                if(now[j]=='\\'&&!flag){
                    flag=true;
                    continue;
                }
                if(now[j]=='\\'&&flag)
                    flag=false;
                cout<<now[j];
            }
            cout<<endl;
        }
    }
    //system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/GarrettWale/p/11455086.html