程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模擬)

ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模擬)

編輯:C++入門知識

ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模擬)


http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5380
Hierarchical Notation


Time Limit: 2 Seconds Memory Limit: 131072 KB

In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a hierarchical data format that uses human-readable text to transmit data objects consisting of attribute-value pairs. The EON was invented by Edward, the headmaster of Marjar University.

The EON format is a list of key-value pairs separated by comma ",", enclosed by a couple of braces "{" and "}". Each key-value pair has the form of "":"". is a string consists of alphabets and digits. can be either a string with the same format of , or a nested EON.

To retrieve the data from an EON text, we can search it by using a key. Of course, the key can be in a nested form because the value may be still an EON. In this case, we will use dot "." to separate different hierarchies of the key.

For example, here is an EON text:

{"headmaster":"Edward","students":{"student01":"Alice","student02":"Bob"}}

    For the key "headmaster", the value is "Edward".For the key "students", the value is {"student01":"Alice","student02":"Bob"}.For the key "students"."student01", the value is "Alice".

    As a student in Marjar University, you are doing your homework now. Please write a program to parse a line of EON and respond to several queries on the EON.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an EON text. The number of colons ":" in the string will not exceed 10000 and the length of each key and non-EON value will not exceed 20.

    The next line contains an integer Q (0 <= Q <= 1000) indicating the number of queries. Then followed by Q lines, each line is a key for query. The querying keys are in correct format, but some of them may not exist in the EON text.

    The length of each hierarchy of the querying keys will not exceed 20, while the total length of each querying key is not specified. It is guaranteed that the total size of input data will not exceed 10 MB.

    Output

    For each test case, output Q lines of values corresponding to the queries. If a key does not exist in the EON text, output "Error!" instead (without quotes).

    Sample Input

    1
    {"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"}}
    4
    "hm"
    "stu"
    "stu"."stu01"
    "students"
    

    Sample Output

    "Edward"
    {"stu01":"Alice","stu02":"Bob"}
    "Alice"
    Error!
    

    Author: LU, Yi


    題意:

    模擬python的字典。給出一個字典,然後有1000個查詢,輸出對應的鍵值。

    分析:

    簡單粗暴的字符串神煩模擬。因為打的網絡同步賽,內存限制得很死,采用了離線操作。

    抓住這幾個關鍵的符號:‘{’ '}' ',' ':' 進行操作。具體見代碼及注釋,數據不是非常惡心。


    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    #define maxn 400007
    
    char buff[maxn];
    
    map > MAP;
    
    char buf[maxn];
    
    void print(int pos)
    {
        int rest=0;
        while ((buff[pos]!=','&& buff[pos]!='}' || rest>0) && buff[pos]!='\0')
        {
            if (buff[pos]=='{')
                rest++;
            if (buff[pos]=='}')
                rest--;
    
            putchar(buff[pos++]);
        }
        putchar('\n');
    }
    
    int ans[1007];
    
    void analyse(const char *s)
    {
        int j=0;
    
        for (int i=0;s[i]!='\0';i++)
        {
            if (s[i]=='{')
            {
                while (j>0 && buf[j-1]!='|') buf[j--]='\0'; // 先往前刪到鍵
                if (j>0 && buf[j-1]=='|') j--;
                buf[j]='\0';
                buf[j++]='.';
                buf[j]='\0';
                continue;
            }
    
            if (s[i]=='}')  //刪掉兩層
            {
                while (j>0 &&buf[j-1]!='.') buf[j--]='\0';
                buf[j--]='\0';
                while (j>0 &&buf[j-1]!='.') buf[j--]='\0';
                buf[j]='\0';
    
                continue;
            }
            if (s[i]==':')
            {
                buf[j]='\0';
                if (MAP.count(string(buf))==1)  //如果是要查詢的,把輸出位置記錄
                {
                    for (int k=0;k


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved