ZOJ 3826 Hierarchical Notation(Mudanjiang 2014)(JSON解析模拟)
来源:互联网 发布:vpn服务器架设软件 编辑:程序博客网 时间:2024/05/16 14:04
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
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
题意:JSON解析,对于查询的key值,输出相应的value值。
思路:注意细节。
代码:
#include <iostream>#include <cstdio>#include <string>#include <map>using namespace std;const int MAXN = 10010;const int ROOT = 0;map<string, int> mp[MAXN];int T, Q, curPos, mpCnt;string text;int parse(int cnt, int deep) { mp[cnt].clear(); if (text[curPos] == '{') { // multiple while (text[curPos++] != '}') { if (text[curPos] == '"') { int startPos = curPos, len = 1; while (text[++curPos] != '"') len++; string key = text.substr(startPos, len + 1); if (text[++curPos] == ':') { curPos++; mp[cnt][key] = parse(++mpCnt, deep + 1); } } } } else { // single if (text[curPos] == '"') { int startPos = curPos, len = 1; while (text[++curPos] != '"') len++; string value = text.substr(startPos, len + 1); mp[cnt][value] = -1; } } return cnt;}void print(int father) { if (mp[father].size() == 1 && mp[father].begin()->second == -1) { cout << mp[father].begin()->first; return; } cout << '{'; map<string, int>::iterator it; for (it = mp[father].begin(); it != mp[father].end(); it++) { cout << it->first << ':'; print(it->second); if (it != --mp[father].end()) cout << ','; } cout << '}';}void init() { getline(cin, text); mpCnt = curPos = 0; parse(0, 1);}void solve() { cin >> Q; getline(cin, text); while (Q--) { bool flag = true; int curMp = ROOT; getline(cin, text); curPos = -1; while (++curPos < text.size()) { int startPos = curPos, len = 1; while (curPos < text.size() && text[++curPos] != '.') len++; string key = text.substr(startPos, len); if (mp[curMp].find(key) != mp[curMp].end()) { curMp = mp[curMp][key]; } else { flag = false; break; } } if (flag) { print(curMp); cout << endl; } else { cout << "Error!" << endl; } }}int main() { ios::sync_with_stdio(false); cin >> T; getline(cin, text); while (T--) { init(); solve(); } return 0;}
- ZOJ 3826 Hierarchical Notation(Mudanjiang 2014)(JSON解析模拟)
- zoj 3826 Hierarchical Notation(模拟)
- ZOJ 3826 Hierarchical Notation(2014 Asia Mudanjiang Regional Contest)
- ZOJ 3826 Hierarchical Notation(模拟-字符串处理)
- zoj 3826 Hierarchical Notation(模拟)
- zoj 3826 Hierarchical Notation(模拟)
- ZOJ 3826 Hierarchical Notation 模拟
- zoj 3826 Hierarchical Notation 模拟
- ZOJ 3826 Hierarchical Notation 模拟
- ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模拟)
- ZOJ 3826 Hierarchical Notation
- ZOJ 3826 Hierarchical Notation
- ZOJ 3826Hierarchical Notation
- ZOJ 3829 Known Notation(模拟+YY)——The 2014 ACM-ICPC Asia Mudanjiang Regional Cont
- ZOJ-3826-Hierarchical Notation【哈希】
- zoj 3829 Known Notation (The 2014 ACM-ICPC Asia Mudanjiang Regional Contest K题) 贪心
- ZOJ 3826 Hierarchical Notation(2014 牡丹江 D,概率DP)
- Hierarchical Notation ZOJ
- ActivityManager的作用
- 项目经验之:针对昨日FDO连接SDF文件报错,国个GIS论坛给出了一个思路.帮助我过关.
- Java的观察者模式—— 一个通俗认识
- win8.1下tomcat7解压缩版配置
- Java Chaos Game 噪声游戏两则
- ZOJ 3826 Hierarchical Notation(Mudanjiang 2014)(JSON解析模拟)
- 魅族阿里合作背后 打响移动互联网入口争夺战
- 【软工视频】--软件开发过程
- bitmap压缩问题(按比例压缩和压缩成固定大小)
- 策略模式与简单工厂模式的结合使用
- C++析构函数,复制构造函数,默认构造函数是执行机制
- 图片缩放裁剪
- dell2950服务器安装
- 经典中的品味:第二章 C++基本的对象,类型和值(上)