Hierarchical Notation ZOJ

来源:互联网 发布:actor critic知乎 编辑:程序博客网 时间:2024/05/16 18:37

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 Tindicating 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.

<h4< dd="">
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).

<h4< dd="">
Sample Input
1{"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"}}4"hm""stu""stu"."stu01""students"
<h4< dd="">
Sample Output
"Edward"{"stu01":"Alice","stu02":"Bob"}"Alice"

Error!

学了哈希的用法 把每一个字符串转换成一个数字来形成映射

还有就是里面找对应括号用到了深搜 遇到左括号就进入下一层搜索直到遇到右括号再回到上一层

map中的count用于返回key出现的次数 在map中只可能为0或1

#include <iostream>#include <map>#include <vector>#include <algorithm>#include<cstdio>using namespace std;const int maxn = 2000000;const int x=123;long long start;char s[maxn],op[maxn];typedef pair<int,int>pos;map<long long,pos>g;int idx(char ch){    if (ch >= '0' && ch <= '9')        return ch - '0';    else if (ch >= 'A' && ch <= 'Z')        return ch - 'A' + 10;    else if (ch >='a' && ch <= 'z')        return ch - 'a' + 36;    else if (ch == '.')        return 62;    return 63;}void solve(long long u){    long long tmp=u;    while(s[start]!='}')    {       start++;       if(s[start]=='}')       return ;       u=tmp;       while(s[start]!=':')          u=u*x+idx(s[start++]);    //确定Key值       int l=++start;        //对应字符串的起始位置       if(s[start]=='{')          solve(u*x+62LL);       else          while(s[start+1]!=','&&s[start+1]!='}')              start++;        g[u]=make_pair(l,start);        start++;    }}int main(){    int t;    cin>>t;    while(t--)    {        scanf("%s",s);        g.clear();        start=0;        solve(0);        int q;        scanf("%d",&q);        for(int i=0;i<q;i++)        {            scanf("%s",op);            long long tmp=0;            for(int i=0;op[i]!='\0';i++)                tmp=tmp*x+idx(op[i]);            if(g.count(tmp))   //结果为0或1            {                pos u=g[tmp];   //注意这个用法                for(int i=u.first;i<=u.second;i++)                    printf("%c",s[i]);                printf("\n");            }            else                printf("Error!\n");        }    }    return 0;}

原创粉丝点击