您的位置:首页 > 其它

ZOJ-3826-Hierarchical Notation【哈希】

2015-10-03 15:25 295 查看

ZOJ-3826-Hierarchical Notation

[code]                        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!

题目大意:“key”:”value”,给出一段字符串,然后q个询问,输入key值,要求输出对应value值。

题目思路:学习了大神的代码。哈希+深搜

题目链接:ZOJ 3826

以下是代码:

[code]#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
#define MOD 123
string s;
map <int,pair < int, int> > mp;   //第一个值存的是key哈希值,pair里面存的是<value起始下标,value结束下标> 
int res = 0;
void dfs(int now)                   
{   
    while(s[res] != '}')
    {
        if (s[++res] == '}') return;
        int t = now;                    //设置根
        while(s[res] != ':') t = t * MOD + s[res++];    //哈希key值        
        int x = ++res;                                  //设置value起始下标 
        if (s[res] == '{') dfs(t * MOD + '.');          //如果存在多层,加上'.'的值,继续深搜 
        else while(s[res + 1] != '}' && s[res + 1] != ',') res++;   //否则,计算value的结束下标 
        mp[t] = make_pair(x,res);
        res++;  
    }
}
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        cin >> s;
        mp.clear();
        res = 0;
        dfs(0);     
        int q;
        cin >> q;
        while(q--)
        {
            string temp;
            cin >> temp;
            int num = 0;
            for (int i = 0; i < temp.size(); i++) num = num * MOD + temp[i];     //计算temp的哈希值 
            if (!mp.count(num)) cout << "Error!\n";
            else
            {
                int num1 = mp[num].first,num2 = mp[num].second;                 //找到起始下标和终止下标输出 
                for (int i = num1; i <= num2; i++) cout << s[i];
                cout << endl;
            }

        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: