ZOJ 3826 Hierarchical Notation Simulation

Source: Internet
Author: User


Simulation: parsing of the grammar

Hash all key construction planning, for recording at several locations the original string of each node begins the output.

。。

On each of the queries along the graph walk can be.

。。


Hierarchical Notation Time limit: 2 Seconds Memory Limit: 131072 KB

In Marjar University, students in College of computer science would learn EON (Edward Object Notation), which is a hierarch ical data format is 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 "<key>": "<value>". <key> is a string consists of alphabets and digits. <value> can is either a string with the same format of <key>, 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 is in a nested form because the value is still an EON. In this case, we'll use dot "." to separate different hierarchies of the key.

For example, this is an EON text:

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

    • For the key "headmaster" and 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 is doing your homework now. Please write a program to parse a line of EON and respond to several queries on the EON.

Input

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

The first line contains a EON text. The number of colons ":" In the string would not be exceed 10000 and the length of each key and Non-eon value would not exceed 20.

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

The length of each hierarchy of the querying keys would not be exceed, while the total length of all querying key is not s Pecified. It is guaranteed, the total size of input data would not be exceed ten 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
Source:The acm-icpc Asia Mudanjiang regional Contest




#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <stack>using namespace std;typedef long long int LL;    ll Hash (char* str) {ll ret=0;    int Len=strlen (str);    for (int i=0;i<len;i++) {ret=ret*123+ (LL) (str[i]-' 0 '); } return ret;} map<ll,int> mp;stack<int> Stk;char str[400007],que[400007];bool graph[10000][10000];int stpt[400007];    void Init () {mp.clear (); while (!stk.empty ()) Stk.pop (); memset (graph,false,sizeof (graph));}    int main () {int t_t;    scanf ("%d", &t_t);        while (t_t--) {init ();        scanf ("%s", str);        int Len=strlen (str);        int word=0;        BOOL Readname=false; Char name[50];        int na;            for (int i=0;i<len;i++) {if (str[i]== ' {') stk.push (word);            else if (str[i]== '} ') Stk.pop ();                   else if (str[i]== ' "') {if (Readname==false) { Readname=true; memset (name,0,sizeof (name));                na=0;                    } else if (readname==true) {readname=false;                    LL Id=hash (name);                    word++;                    Mp[id]=word;                    stpt[mp[id]]=i+1;                Graph[stk.top ()][word]=true;                }} else if (str[i]== ': ') {if (str[i+1]== ' {') continue;                    else if (str[i+1]== ' "') {i++;                    while (str[i+1]!= ' "') i++;                i++;            }} else if (readname==true) {name[na++]=str[i];        }} int m;        scanf ("%d", &m);            while (m--) {scanf ("%s", Que);            int Len2=strlen (que);            BOOL Flag=true; na=0;            int p=0;          Readname=false;  for (int i=0;i<len2&&flag;i++) {if (que[i]== ' ") {                        if (readname==false) {na=0; memset (name,0,sizeof (name));                    Readname=true;                        } else if (readname==true) {readname=false;                        LL Id=hash (name);                        if (graph[p][mp[id]]==true) {P=mp[id];                    } else Flag=false;                }} else if (que[i]== '. ') continue;            else Name[na++]=que[i];            } if (Flag==false) puts ("error!");                else {char cc=str[stpt[p]+1];                int dep=0;                if (cc== ' {') cc= '} ';                int ii=stpt[p]+1;       if (cc== ' "') {ii++; Putchar ('" ');}         while (true) {if (cc== '} ') {if (str[ii                        ]== ' {') dep++;                        if (str[ii]== '} ') dep--;                    if (dep==0) break;                    } else if (cc== ' "') {if (str[ii]== '") break; } putchar (Str[ii]);                ii++;                } putchar (cc);            Putchar (10); }}} return 0;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

ZOJ 3826 Hierarchical Notation Simulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.