Zoj 3826 hierarchical notation (2014 Mudanjiang H, string simulation)

Source: Internet
Author: User

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 hosted ted 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 be 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 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 integerTIndicating 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 integerQ(0 <=Q<= 1000) indicating the number of queries. Then followedQLines, 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, outputQLines 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


Question:

Simulate the python dictionary. A dictionary is provided, and then there are 1000 queries, and the corresponding key value is output.

Analysis:

A simple and crude string is annoying to simulate. Because of the network synchronization competition, the memory limit is very high and offline operations are adopted.

Capture these key symbols. For details, see the code and comments. The data is not very disgusting.


#include <cstdio>#include <cmath>#include <ctime>#include <cstdlib>#include <cstring>#include <algorithm>#include <string>#include <vector>#include <map>using namespace std;#define maxn 400007char buff[maxn];map<string,vector<int> > 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<MAP[(string)buf].size();k++)                    ans[MAP[(string)buf][k]]=i+1;            }            buf[j++]='|'; //'|'用来分割            buf[j]='\0';            continue;        }        if (s[i]==',')        {            while (buf[j-1]!='.') buf[j--]='\0';  //删掉一层            continue;        }        buf[j++]=s[i];        buf[j]='\0';    }}char query[maxn];int main(){    query[0]='.';    int T_T;    scanf("%d",&T_T);    while (T_T--)    {        MAP.clear();        scanf("%s",buff);        int q;        scanf("%d",&q);        memset(ans,-1,sizeof ans);        for (int i=1;i<=q;i++)        {            scanf("%s",query+1);            MAP[string(query)].push_back(i);        }        analyse(buff);        for (int i=1;i<=q;i++)            if (ans[i]==-1)                puts("Error!");            else                print(ans[i]);    }    return 0;}



Zoj 3826 hierarchical notation (2014 Mudanjiang H, string 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.