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
思路:注意到我们能根据输入构造出一颗树的,我用Node表示树上的结点,每个Node里面是一个string key和map<string,Node*> value和value在输入的起始位置和结束位置。 这样询问的时候我们能从树的根开始往下走就行了。不过预处理感觉比较麻烦....注意输入有这种情况的 {"a":{}}
代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <stdio.h>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <cmath>
#include <time.h>
#include <cassert>
#include <math.h>
#define mp make_pair
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,b,a) for(int i = (b); i >= (a); --i)
#define clr(a,x) memset(a,(x),sizeof(a))
#define LL long long
#define eps 1e-10
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bit(x) (1LL<<(x))
using namespace std;
const int maxn = 50000+5;
struct Node
{
string key;
map<string,Node*> value;
int l,r;
}src[maxn],*rt;
int c;
inline Node * new_node()
{
src[++c].key = "";
src[c].value.clear();
return &src[c];
}
char in[maxn*20];
char s[maxn*20];
Node * fa[maxn];
void input()
{
c = 0; rt = new_node();
rt->key = ""; rt->value.clear();
scanf("%s",s);
memcpy(in,s,sizeof(s));
int len = strlen(s);
int t = 0; fa[t++] = rt;
string key = "";
Node * ch = rt;
vector<int> backup;
rep(i,0,len) {
if (s[i] == '"') {
ch = new_node();
fa[t++] = ch;
int j = i;
while (s[++i] != '"') ch->key += s[i];
if (t - 2 >= 0) fa[t-2]->value[ch->key] = ch;
if (s[i+1] == ',' || s[i+1] == '}') {
--t;
fa[t-1]->l = j;
fa[t-1]->r = i+1;
}
} else if (s[i] == '{') {
fa[t-1]->l = i;
backup.push_back(t);
}
else if (s[i] == '}') {
t = backup.back(); backup.pop_back();
fa[t-1]->r = i+1;
}
else if (s[i] == ',') {
--t;
}
}
}
void solve()
{
int Q; scanf("%d",&Q);
while (Q--) {
scanf("%s",s);
int len = strlen(s);
string key = "";
Node * now = rt;
bool ok = true;
rep(i,0,len) {
if (s[i] == '"') {
key = "";
while (s[++i] != '"') key += s[i];
if (now->value.count(key) == 0) { puts("Error!"); ok = false; break; }
else now = now->value[key];
}
}
if (ok) {
int l = now->l, r = now->r;
char ch = in[r]; in[r] = '\0';
printf("%s\n",in+l);
in[r] = ch;
}
}
}
int main()
{
int T; cin >> T;
while (T--) {
input();
solve();
}
}