ZOJ3826(Hierarchical Notation)

来源:互联网 发布:淘宝打包员招聘要求 编辑:程序博客网 时间:2024/06/05 15:35

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 "<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 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!


给定一个字符串,描述的是key值和value值,冒号前面是key值,冒号后面是value值,对于每一个key值,冒号后面的全部字符直至下一个逗号前,都是其的value值,有可能包含大括号,多个内容。

输入key值,输出对应的value值。

一开始用的字典树去做的,提交之后一直返回segment fault。查题解之后发现根本不用建树,只用对于每一个key值存下他的value值的起止位置,用hash做一个map就好了。

代码如下:

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <map>using namespace std;const int N=500000;const int p=140;//只要大过字符串中最大的字符就好char query[N];char str[N];map<unsigned long long,pair<int ,int> > m;int ind;void fun(unsigned long long num){    unsigned long long tmp=num;    while(str[ind]!='}')    {        ind++;                if(str[ind]=='}')//函数递归的出口            return ;                    num=tmp;//每次分块后要对num进行重新赋值        while(str[ind]!=':')        {            num=num*p+str[ind];//key值的hash            ind++;        }        ind++;        int l=ind;        if(str[ind]=='{')            fun(num*p+'.');//遇见有多个内容的key值        else        {            while(str[ind+1]!=','&&str[ind+1]!='}')                ind++;        }        m[num]=make_pair(l,ind);//存左右        ind++;    }}int main(){    int t,q;    scanf("%d",&t);    while(t--)    {        m.clear();        ind=0;        scanf("%s",str);        fun(0);        scanf("%d",&q);        while(q--)        {            scanf("%s",query);            int len=strlen(query);            unsigned long long int ans=0;            for(int i=0;i<len;i++)            {                ans=ans*p+query[i];//hash查找            }            if(m.count(ans))            {                pair<int,int> p=m[ans];                for(int i=p.first;i<=p.second;i++)                    printf("%c",str[i]);                printf("\n");            }            else                printf("Error!\n");        }    }    return 0;}



原创粉丝点击