您的位置:首页 > 其它

ZOJ 3826 Hierarchical Notation(亚洲区域赛牡丹江站 字符串模拟)

2014-10-20 16:04 176 查看
Hierarchical NotationTime Limit: 2 Seconds      Memory Limit: 131072 KBIn 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经过艰苦卓绝的战斗。。。。终于出来了。。。这道题
#include <iostream>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include<map>#include <algorithm>#define esp 1e-6#define inf 0x0f0f0f0f#define LL long long  using namespace std;#define maxn 400007char papapa[maxn];char buf[maxn];char query[maxn];int ans[1007];map<string,vector<int> > MAP;void getans(const char *s){	int i,j,k,l,n,m;	j=0;	for(i=0;s[i]!='\0';i++)	{        if(s[i]=='{')          {              while(j>0 && buf[j-1]!='|') buf[j--]='\0';             if (j>0 && buf[j-1]=='|') j--;              buf[j]='\0';              buf[j++]='.';              buf[j]='\0';              continue;          }          if (s[i]=='}')          {              while (j>0 &&buf[j-1]!='.') buf[j--]='\0';              buf[j--]='\0';              while (j>0 &&buf[j-1]!='.') buf[j--]='\0';              buf[j]='\0';                continue;          }          if(s[i]==':')          {              buf[j]='\0';              if(MAP.count(string(buf))==1)            {                  for (k=0;k<MAP[(string)buf].size();k++)                      ans[MAP[(string)buf][k]]=i+1;              }              buf[j++]='|';              buf[j]='\0';              continue;          }          if (s[i]==',')          {              while (buf[j-1]!='.') buf[j--]='\0';               continue;          }          buf[j++]=s[i];          buf[j]='\0';      }  }int main(){	query[0]='.';	int i,j,k,l,m,n,t;	scanf("%d%*c",&t);	while(t--)	{        MAP.clear();          scanf("%s",papapa);          int q;          scanf("%d",&q);           memset(ans,-1,sizeof ans);  		for(i=1;i<=q;i++)		{            scanf("%s",query+1);  			MAP[string(query)].push_back(i);		}		getans(papapa);  		for (i=1;i<=q;i++)              if(ans[i]==-1)                  puts("Error!");              else			{   				int rest=0;  			    while ((papapa[ans[i]]!=','&& papapa[ans[i]]!='}' || rest>0) && papapa[ans[i]]!='\0')  			    {  			        if (papapa[ans[i]]=='{')  			            rest++;  			        if (papapa[ans[i]]=='}')  			            rest--;  			  			        putchar(papapa[ans[i]++]);  			    }  			    putchar('\n'); 			}  	}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: