您的位置:首页 > 其它

The 2014 ACM-ICPC Asia Mudanjiang Regional Contest - H

2014-10-16 12:44 337 查看
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!


Author: LU, Yi

第一次使用上map,但是当时不时mle就是sg,看了人家大神的blog要用一个hash,每个key转换成hash,然后再来记录value,过程还是比较麻烦的,估计模拟题都是这样吧。题意比较好懂,每个key对应一个value,不过会存在嵌套,然后进行n次查询,每次输入一个key要求输出value。看了大神的思路,用了递归的思路,因为这样比较好理解。预处理好之后,这道题的难题就解决了。

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL unsigned long long
#define inf 0x0f0f0f0f
using namespace std;
#define max 110
typedef pair<int,int>pp;
map<LL,pp> Map;
char s[2000005];
char q[2000005];
int pos;
void solve(LL u)
{
LL tmp;
int l;
while(s[pos]!='}')
{
tmp=u;
pos++;
if(s[pos]=='}')
return;
while(s[pos]!=':')
{
tmp=tmp*123+(LL)(s[pos++]-'0');//hash处理
}
pos++;
l=pos;
if(s[pos]=='{')
solve(tmp*123+(LL)('.'-'0'));//{当作.来处理,方便后面的查询。
else
{
while(s[pos+1]!=','&&s[pos+1]!='}')
pos++;
}
Map[tmp]=make_pair(l,pos);
pos++;
}
}
int main()
{
int t,i,j;
int n,l,l2;
int s1,e1,s2,e2;
LL jl=0;
scanf("%d",&t);
while(t--)
{
Map.clear();
scanf("%s",s);
pos=0;
solve(0);
scanf("%d",&n);
while(n--)
{
jl=0;
scanf("%s",q);
l2=strlen(q);
for(i=0;i<l2;i++)
jl=jl*123+(LL)(q[i]-'0');
if(Map.find(jl)!=Map.end())
{
pp ans;
ans=Map[jl];
//printf("***%d %d\n",ans.first,ans.second);
for(i=ans.first;i<=ans.second;i++)
printf("%c",s[i]);
printf("\n");
}
else
printf("Error!\n");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: