Skip to content

Pro3

原始文件为 CPP 代码,本文是转换后的 Markdown 文件。

#include <iostream>
#include <cstring>
#include <string>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;

typedef map<string,string> Obj;
vector<Obj> objcontainer;

void dealwith(int pos,string& content,string& keystr)
{
    char c;
    while((c = content[pos++]) != '"')
    {
        if (c == '\\')
        {
            c = content[pos++];
        }
        keystr += c;
    }   
}

void readObj(int& pos, string& content)
{
    Obj thisobj;
    thisobj.clear();
    while(true)
    {
        string keystr = "";
        string valuestr = "";

        char c;
        while(c = content[pos++])
        {
            if (c == '"') break;
            else if (c == '}')
            {
                objcontainer.push_back(thisobj);
                return;
            }
        }
        dealwith(pos,content,keystr);
        while(content[pos++] != ':');

        while(c = content[pos++])
        {
            if (c == '"')
            {
                dealwith(pos,content,valuestr); 
                //cout << "test" << endl;
                break;      
            }else if (c == '{')
            {
                readObj(pos,content);
                valuestr += ".";
                valuestr += to_string(objcontainer.size()-1);
                valuestr += "\0";
                break;
            }
        }
        //cout << keystr << ":" << valuestr << endl;
        thisobj.insert(make_pair(keystr,valuestr));
        while(c = content[pos++])
        {
            if (c == ',')
            {
                break;
            }
            else if (c == '}')
            {
                objcontainer.push_back(thisobj);
                return;
            }
        }
    }
}



int main()
{
    objcontainer.clear();
    //freopen("in.txt","r",stdin);
    int n,m;
    scanf("%d%d",&n,&m);
    cin.get();

    string content = "";
    string line;
    for (int i = 0; i < n; ++i)
    {
        getline(cin,line);
        content += line;
    }

    int pos = 0;
    readObj(pos,content);

    int num = objcontainer.size()-1;
//  for (int i = 0; i < num; ++i)
//  {
//      Obj thisobj = objcontainer[i];
//      cout << "Number " << i << endl;
//      for (auto x=thisobj.begin(); x != thisobj.end(); ++x)
//      {
//          cout << x->first << " : " << x->second << endl;
//      }
//  }

    for (int i = 0; i < m; ++i)
    {
        Obj objsearch = objcontainer[num];
        getline(cin,line);
        stringstream ss(line);
        string item;

        bool flag = false;
        while(getline(ss,item,'.'))
        {
            if (!objsearch.count(item) > 0)
            {
                cout << "NOTEXIST" << endl;
                flag = false;
                break;
            }

            string result = objsearch[item];
            if (result[0] == '.')
            {
                result = result.substr(1,result.size());
                objsearch = objcontainer[stoi(result)];
                flag = true;
            }else
            {
                if (!getline(ss,item,'.'))
                {
                    cout << "STRING " << result << endl;
                    flag = false;
                }else
                {
                    cout << "NOTEXIST" << endl;
                    flag = false;
                }
                break;
            }
        }

        if (flag)
        {
            cout << "OBJECT" << endl;
        }   
    }   
}