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 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. 1
  2. {"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"}}
  3. 4
  4. "hm"
  5. "stu"
  6. "stu"."stu01"
  7. "students"

Sample Output

  1. "Edward"
  2. {"stu01":"Alice","stu02":"Bob"}
  3. "Alice"
  4. Error!
  5.  
  6. 题意: 牡丹江的H题, 写一个很类似python的字典的东西,然后给出键值输出对应的值,可以嵌套。ps 好喜欢python这样的自由、简洁,打完这星期国赛就专研一下python
  7.  
  8. 思路: 使劲模拟一遍就ok啦,一开始tle了,看了别人题解,原来应该hash一下字符串再存入map中的,改hash150ms就过了
  1. #pragma comment(linker,"/STACK:1024000000,1024000000")
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. const int N = ;
  5.  
  6. char s[N],query[N];
  7. int p,sz,qsz;
  8.  
  9. struct _node{
  10. int val_l,val_r;
  11. map<string,int> mp;
  12. void clear()
  13. {
  14. val_l=val_r=;
  15. mp.clear();
  16. }
  17. }node[N];
  18. int cnt;
  19.  
  20. void readname(char *s,int sz,int &l,int &r)
  21. {
  22. l=p++;
  23. while(p<sz && s[p]!='"')
  24. p++;
  25. r=p++;
  26. // cout<<l<<' '<<r<<' '<<s[l]<<' '<<s[r]<<endl;
  27. }
  28.  
  29. void prints(int l,int r)
  30. {
  31. while(l<=r && l<sz) putchar(s[l++]);
  32. puts("");
  33. }
  34.  
  35. void build(int root)
  36. {
  37. node[root].val_l=p++;
  38. if(s[p]=='}') goto bk;
  39. while(p<sz)
  40. {
  41. int l,r;
  42. readname(s,sz,l,r);
  43. string key(s,l,r-l+);
  44. // cout<<l<<' '<<r<<' '<<root<<' '<<key<<endl;
  45. node[root].mp[key]=cnt;
  46. // cout<<node[root].mp[key]<<endl;
  47. node[cnt++].clear();
  48. p++; // :
  49. if(s[p]=='{')
  50. build(cnt-);
  51. else
  52. readname(s,sz,node[cnt-].val_l,node[cnt-].val_r);
  53. if(s[p]=='}') break;
  54. else p++; // ,
  55. }
  56. bk:
  57. node[root].val_r=p++;
  58. }
  59.  
  60. void getans(int root,int &ansl,int &ansr)
  61. {
  62. int l,r;
  63. while(true)
  64. {
  65. readname(query,qsz,l,r);
  66. string str(query,l,r-l+);
  67. if(node[root].mp.find(str)!=node[root].mp.end())
  68. root=node[root].mp[str];
  69. else
  70. {
  71. root=-;
  72. break;
  73. }
  74. if(query[p]!='.') break;
  75. p++;
  76. }
  77. if(root==-) ansl=ansr=-;
  78. else ansl=node[root].val_l,ansr=node[root].val_r;
  79. }
  80.  
  81. void run()
  82. {
  83. // scanf("%s",s);
  84. gets(s);
  85. p=,sz=strlen(s);
  86. node[].clear();
  87. cnt=;
  88. build();
  89. int q;char c;
  90. scanf("%d",&q);
  91. scanf("%c",&c);
  92. while(q--)
  93. {
  94. // scanf("%s",query);
  95. gets(query);
  96. qsz=strlen(query);
  97. p=;
  98. int l,r;
  99. getans(,l,r);
  100. if(l==-)
  101. puts("Error!");
  102. else
  103. prints(l,r);
  104. }
  105. // cout<<endl;
  106. // for(map<string,int>::iterator it=node[0].mp.begin(); it!=node[0].mp.end(); it++)
  107. // cout<<it->first<<' '<<it->second<<endl;
  108. }
  109.  
  110. void test()
  111. {
  112. scanf("%s",s);
  113. p=;
  114. prints(,);
  115. }
  116.  
  117. int main()
  118. {
  119. #ifdef LOCAL
  120. freopen("in","r",stdin);
  121. #endif
  122. // test();
  123. int _;char c;
  124. scanf("%d",&_);
  125. scanf("%c",&c);
  126. while(_--)
  127. run();
  128. return ;
  129. }

不过还是这个大神写的比较简洁耗内存少

http://blog.csdn.net/keshuai19940722/article/details/40039745

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <map>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8. typedef unsigned long long ll;
  9. typedef pair<int,int> pii;
  10.  
  11. const int maxn = ;
  12. const ll x = ;
  13.  
  14. int N, Q, mv;
  15. char op[maxn], s[maxn];
  16. map<ll, pii> G;
  17.  
  18. inline int idx(char ch) {
  19. if (ch >= '' && ch <= '')
  20. return ch - '';
  21. else if (ch >= 'A' && ch <= 'Z')
  22. return ch - 'A' + ;
  23. else if (ch >= 'a' && ch <= 'z')
  24. return ch - 'a' + ;
  25. else if (ch == '.')
  26. return ;
  27. return ;
  28. }
  29.  
  30. void solve (ll u) {
  31. ll tmp = u;
  32.  
  33. while (s[mv] != '}') {
  34. mv++;
  35. if (s[mv] == '}')
  36. return;
  37. u = tmp;
  38.  
  39. while (s[mv] != ':')
  40. u = u * x + idx(s[mv++]);
  41.  
  42. int l = ++mv;
  43.  
  44. if (s[mv] == '{')
  45. solve(u * x + 62LL);
  46. else
  47. while (s[mv+] != ',' && s[mv+] != '}') mv++;
  48.  
  49. G[u] = make_pair(l, mv);
  50. mv++;
  51. }
  52. }
  53.  
  54. int main () {
  55. int cas;
  56. scanf("%d", &cas);
  57. while (cas--) {
  58. scanf("%s", s);
  59.  
  60. mv = ;
  61. G.clear();
  62. solve();
  63.  
  64. scanf("%d", &Q);
  65. for (int i = ; i <= Q; i++) {
  66. scanf("%s", op);
  67.  
  68. ll ret = ;
  69. int len = strlen(op);
  70.  
  71. for (int i = ; i < len; i++)
  72. ret = ret * x + idx(op[i]);
  73.  
  74. if (G.count(ret)) {
  75. pii u = G[ret];
  76. for (int i = u.first; i <= u.second; i++)
  77. printf("%c", s[i]);
  78. printf("\n");
  79. } else
  80. printf("Error!\n");
  81. }
  82. }
  83. return ;
  84. }

zoj3826 Hierarchical Notation (字符串模拟)的更多相关文章

  1. ZOJ 3826 Hierarchical Notation 模拟

    模拟: 语法的分析 hash一切Key建设规划,对于记录在几个地点的每个节点原始的字符串开始输出. . .. 对每一个询问沿图走就能够了. .. . Hierarchical Notation Tim ...

  2. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  3. HDU-3787(字符串模拟)

    Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出.   Input 输入包含 ...

  4. 2014牡丹江——Hierarchical Notation

    problemId=5380" style="background-color:rgb(51,255,51)">题目链接 字符串模拟 const int MAXN ...

  5. HDU-1002.大数相加(字符串模拟)

    本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...

  6. Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)

    题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...

  7. HDU-Digital Roots(思维+大数字符串模拟)

    The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...

  8. Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)

    P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...

  9. CCF(JSON查询:40分):字符串+模拟

    JSON查询 201709-3 纯字符串模拟,考的就是耐心和细心.可惜这两样我都缺... #include<iostream> #include<cstdio> #includ ...

随机推荐

  1. Socket的错误码和描述

    //下面是Socket Error的错误码和描述: Socket error 0 - Directly send error  Socket error 10004 - Interrupted fun ...

  2. Vue中div高度自适应

    Vue中尽量不使用dom的高度计算 <template> <div :style="conheight"> </template> <sc ...

  3. Notepad工具使用小技巧

    工欲善其事必先利其器 Notepad++是个很不错的文本编辑工具,掌握它的使用技巧可以提高我们工作的效率.见如下: 比较常用的罗列如下:(如果有更好的建议可以留言哈) 1: 添加书签 CTRL+F2 ...

  4. cocos2d-js添加广点通插屏(通过jsb反射机制)

    1.把广点通的jar包加入libs文件夹 2.修改AndroidManifest.xml文件 添加权限: <uses-permission android:name="android. ...

  5. Golang 环境变量及工作区概念

    GOROOT go的安装路径 GOPATH 可以有多个目录,每个目录就是一个工作区,放置源码文件,以及安装后的归档文件和可执行文件: 第一个工作区比较重要,go get会自动从一些主流公用代码仓库下载 ...

  6. POJ - 1321 棋盘问题 【DFS】

    题目链接 http://poj.org/problem?id=1321 思路 和N皇后问题类似 但是有一点不同的是 这个是只需要摆放K个棋子就可以了 所以 我们要做好 两个出口 并且要持续往下一层找 ...

  7. tomcat 1)启动时不识别执行启动命令 2)启动报错 3)关闭不了,用myEclipse启动时显示jvm_bind,端口占用

  8. vue-awesome-swiper的使用以及API整理

    一.先说一个看关于vue-awesome-swiper的一个坑 vue项目的package.json中显示的"vue-awesome-swiper": "^2.5.4&q ...

  9. 15 个最佳的 jQuery 布局插件【转】

    英文原文:15 Best jQuery Grid Plugins for Developers 现如今,网站开发设计的需求会要求自动适应所有移动设备,即响应式网站: 在开发网站时必须考虑对平板设备融合 ...

  10. 分享知识-快乐自己:自定义struts2类型转换的全局与局部错误信息。

    遇到类型转换错误的时候(也就是说不能进行类型转换),struts2框架自动生成一条错误信息,并且将该错误信息放到addFieldError里面.我们可以通过配置文件来替换这条由struts2自动生成的 ...