Simulation: Grammar Analysis
Create a map for each key in hash, and output the records of each node at the first position of the original string ....
You can go along the graph of each query ....
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 hosted ted 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 integerTIndicating 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 integerQ(0 <=Q<= 1000) indicating the number of queries. Then followedQLines, 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, outputQLines 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
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <stack>using namespace std;typedef long long int LL;LL hash(char* str){ LL ret=0; int len=strlen(str); for(int i=0;i<len;i++) { ret=ret*123+(LL)(str[i]-'0'); } return ret;}map<LL,int> mp;stack<int> stk;char str[400007],que[400007];bool graph[10000][10000];int stpt[400007];void init(){ mp.clear(); while(!stk.empty()) stk.pop(); memset(graph,false,sizeof(graph));}int main(){ int T_T; scanf("%d",&T_T); while(T_T--) { init(); scanf("%s",str); int len=strlen(str); int word=0; bool readname=false; char name[50]; int na; for(int i=0;i<len;i++) { if(str[i]=='{') stk.push(word); else if(str[i]=='}') stk.pop(); else if(str[i]=='"') { if(readname==false) { readname=true; memset(name,0,sizeof(name)); na=0; } else if(readname==true) { readname=false; LL id=hash(name); word++; mp[id]=word; stpt[mp[id]]=i+1; graph[stk.top()][word]=true; } } else if(str[i]==':') { if(str[i+1]=='{') continue; else if(str[i+1]=='"') { i++; while(str[i+1]!='"') i++; i++; } } else if(readname==true) { name[na++]=str[i]; } } int m; scanf("%d",&m); while(m--) { scanf("%s",que); int len2=strlen(que); bool flag=true; na=0; int p=0; readname=false; for(int i=0;i<len2&&flag;i++) { if(que[i]=='"') { if(readname==false) { na=0; memset(name,0,sizeof(name)); readname=true; } else if(readname==true) { readname=false; LL id=hash(name); if(graph[p][mp[id]]==true) { p=mp[id]; } else flag=false; } } else if(que[i]=='.') continue; else name[na++]=que[i]; } if(flag==false) puts("Error!"); else { char cc=str[stpt[p]+1]; int dep=0; if(cc=='{') cc='}'; int ii=stpt[p]+1; if(cc=='"') { ii++; putchar('"'); } while(true) { if(cc=='}') { if(str[ii]=='{') dep++; if(str[ii]=='}') dep--; if(dep==0) break; } else if(cc=='"') { if(str[ii]=='"') break; } putchar(str[ii]); ii++; } putchar(cc); putchar(10); } } } return 0;}
Zoj 3826 hierarchical notation Simulation