# [2014 Regional]牡丹江 H Hierarchical Notation 做题实录

{"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"},"stu01":"aaa"}
5
"hm"
"stu"
"stu"."stu01"
"stu01"
"students"
{"hm":"Edward","stu":{"stus":{"stu01":"Alice","stu02":"Bob"}},"name":"school"}
5
"hm"
"stu"
"stu"."stu01"
"stu"."stus"
"name"

}},这里的处理没处理好，改一下，然后就3Y了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

char str[500005];
char query[500005];
map<string,int> ms[10005];
char tmp[2000];

struct Node
{
int type;
int sp,ep;
int mapid;
void showme()
{
for(int i=sp;i<=ep;i++)
printf("%c",str[i]);
puts("");
}
}node[10005];

int phrase(char *str,int &p)
{
int idx=0;
do
{
tmp[idx++]=str[p];
p++;
}while(str[p]!='"');
tmp[idx++]='"';
tmp[idx++]='\0';
p++;
return idx;
}

int findbrac(int p)
{
int lbrac=0;
for(int i=p;;i++)
{
if(str[i]=='{')
lbrac++;
else if(str[i]=='}')
lbrac--;
if(lbrac==0)
return i;
}
}

int stack[10005];

int main()
{
int T;
for(scanf("%d%*c",&T);T--;)
{
gets(str);
for(int i=0;i<=10001;i++)
ms[i].clear();
int len=strlen(str),stop=0,mcnt=1;
int ncnt=1,p=1;
stack[0]=0;
for(;p<len-1;p++)
{
if(str[p]==',')continue;
if(str[p]=='}')
{
stop--;
continue;
}
phrase(str,p);
p++;
if(str[p]=='"')
{
node[ncnt].type=0;
node[ncnt].sp=p;
ms[stack[stop]][tmp]=ncnt;
phrase(str,p);
node[ncnt].ep=p-1;
ncnt++;
}
else
{
node[ncnt].type=1;
node[ncnt].sp=p;
node[ncnt].ep=findbrac(p);
node[ncnt].mapid=mcnt++;
ms[stack[stop]][tmp]=ncnt;
stop++;
stack[stop]=node[ncnt].mapid;
ncnt++;
}
if(str[p]=='}')
stop--;
}
int Q;
for(scanf("%d%*c",&Q);Q--;)
{
gets(query);
int ctmap=0,p=0,len=strlen(query);
for(;p<len;p++)
{
phrase(query,p);
map<string,int>::iterator it=ms[ctmap].find(tmp);
if(it==ms[ctmap].end())
{
puts("Error!");
break;
}
int nid=it->second;
if(query[p]=='.'&&node[nid].type!=1)
{
puts("Error!");
break;
}
if(query[p]=='.')
{
ctmap=node[nid].mapid;
}
else
{
node[nid].showme();
break;
}
}
}
}
return 0;
}