2014牡丹江现场-H-大模拟

大模拟

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <string>
  5 #include <vector>
  6 #include <map>
  7 
  8 using namespace std;
  9 
 10 const int maxn = 1e7+10;
 11 const int max_node = 1e5+10;
 12 
 13 int T,N,len;
 14 char line[maxn];
 15 char text[maxn];
 16 
 17 struct node
 18 {
 19     string contain;
 20     int kind;
 21     int sons;
 22     map<string,int> key;
 23     map<string,int> nxt;
 24     vector <pair<int,int> > val;
 25 }json[max_node];
 26 
 27 int n_eon = 0;
 28 
 29 pair<int,int> dfs(int pos,int fa)
 30 {
 31     json[++n_eon].kind = 0;
 32     json[n_eon].key.clear();
 33     json[n_eon].val.clear();
 34     json[n_eon].nxt.clear();
 35 
 36     int cur_eon = n_eon;
 37 
 38     //printf("%s\n",text+pos);
 39     bool flag_key = false;
 40 
 41     string val;
 42     string key;
 43 
 44     while(pos < len)
 45     {
 46         if(text[pos] == '{' && flag_key)
 47         {
 48             pair<int,int> tmp = dfs(pos+1,n_eon);
 49             int n_pos = tmp.first;
 50             pair<int,int> pr;
 51             pr.first = pos;
 52             pr.second = n_pos;
 53             //val.clear();
 54             //for(int i=pos;i<n_pos;i++)
 55             //{
 56             //    val += text[i];
 57             //}
 58 
 59             pos = n_pos;
 60             flag_key = false;
 61             //printf("02:get val %s\n",val.c_str());
 62             json[cur_eon].val.push_back(pr);
 63             json[cur_eon].nxt.insert(pair<string,int>(key,tmp.second));
 64         }
 65         else if(text[pos] == '}')
 66         {
 67             return pair<int,int>(pos+1,cur_eon);
 68         }
 69         else if(text[pos] == '"')
 70         {
 71             if(flag_key == false)
 72             {
 73                 key.clear();
 74                 while(++pos < len && text[pos] != '"')
 75                 {
 76                     key += text[pos];
 77                 }
 78                 pos++;
 79                 //printf("get key %s cur:%d \n",key.c_str(),cur_eon);
 80                 json[cur_eon].key.insert(pair<string,int>(key,json[cur_eon].val.size()));
 81                 flag_key = true;
 82             }
 83             else
 84             {
 85                 pair<int,int> pr;
 86                 pr.first = pos;
 87                 //val.clear();
 88                 //val+='"';
 89 
 90                 while(++pos < len && text[pos] != '"')
 91                 {
 92                     //val += text[pos];
 93                 }
 94 
 95                 //val+='"';
 96                 pr.second = ++pos;
 97                 //pos++;
 98                 //printf("01:get val %s cur:%d siz:%d\n",val.c_str(),cur_eon,json[cur_eon].val.size());
 99                 json[cur_eon].val.push_back(pr);
100                 flag_key = false;
101             }
102         }
103         else if(text[pos] == ':')
104         {
105             if(flag_key)
106             {
107 
108             }
109             else
110             {
111                 //printf("Error! no key\n");
112             }
113             pos++;
114         }
115         else if(text[pos] == ',')
116         {
117             pos++;
118         }
119         //pos++;
120     }
121 }
122 
123 char op[maxn];
124 int main()
125 {
126     //freopen("input.txt","r",stdin);
127     //freopen("output.txt","w",stdout);
128     scanf("%d ",&T);
129     while(T--)
130     {
131         n_eon = 0;
132         gets(line);
133         len = strlen(line);
134         int cnt = 0;
135         for(int i=0;i<len;i++)
136         {
137             if(line[i] == ' ') continue;
138             else text[cnt++] = line[i];
139         }
140         text[cnt] = 0;
141 
142         len = cnt;
143         dfs(1,0);
144         //printf("load!\n");
145         scanf("%d ",&N);
146         for(int i=0;i<N;i++)
147         {
148             //printf("N:%d i:%d\n",N,i);
149             gets(op);
150             int l = strlen(op),pos = 0;
151             int cur = 1;
152             //printf("q:%s\n",op);
153             while(pos < l)
154             {
155                 if(op[pos] == '"')
156                 {
157                     string key;
158                     while(++pos < l && op[pos] != '"')
159                     {
160                         key += op[pos];
161                     }
162                     //printf("query: %s\n",key.c_str());
163                     pos++;
164                     if(json[cur].key.count(key) != 0)
165                     {
166                         if(op[pos] == '.' && json[cur].nxt.count(key) != 0)
167                         {
168                             cur = json[cur].nxt[key];
169                             pos++;
170                             continue;
171                         }
172                         if(op[pos] == 0)
173                         {
174                             //printf("cur:%d get: %d num:%d\n",cur,json[cur].key[key],json[cur].val.size());
175                             //printf("%s\n",json[cur].val[ json[cur].key[key] ].c_str());
176                             pair<int,int> tp = json[cur].val[ json[cur].key[key] ];
177                             for(int i=tp.first; i < tp.second ; i++)
178                             {
179                                 putchar(text[i]);
180                             }
181                             puts("");
182                             break;
183                         }
184                         else
185                         {
186                             printf("Error!\n");
187                             break;
188                         }
189                     }
190                     else
191                     {
192                         printf("Error!\n");
193                         break;
194                     }
195                 }
196             }
197         }
198     }
199 }

 

posted @ 2016-08-13 16:49  Helica  阅读(189)  评论(0编辑  收藏  举报