ZOJ 3826
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
{"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
赛后看见别人补这个题,我便也补,其实场上没有看这个题,听说是到模拟,我觉得正解应该就是字符串处理来模拟数据库,然而这个题可以更简单,由于数据少,10000条key,所以hush便出现重的几率很小,看见大家都是字符串+hush水的,我也这么做的,总之,这样做简单啊。。。
注意边界处理
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define sfl(n) scanf("%lld", &n)
#define pfi(n) printf("%d\n", n)
#define pfl(n) printf("%lld\n", n)
#define MAXN 1000005
#define BASE 131
char s[MAXN];
map<ll, pair<int, int> > ma;
ll id(char c)
{
return ((ll)c) % BASE;
}
int p;
void Create(ll f)
{
while(s[p] != '}')
{
if(s[++p] == '}') return ;
ll t = f;
while(s[p] != ':')
{
t = t * BASE + id(s[p]);
p++;
}
int st = ++p;
if(s[p] == '{')
{
Create(t * BASE + id('.'));
}
else
{
while(s[p] != ',' && s[p] != '}') p++;
}
if(s[p] == '}')
ma[t] = pair<int, int>(st, p + );
else ma[t] = pair<int, int>(st, p);
//cout<<t<<endl;
}
return ;
} int main()
{
int T;
sfi(T);
while(T--)
{
ma.clear();
scanf("%s", s);
int n;
char t[MAXN];
p = ;
Create();
sfi(n);
repu(i, , n)
{
scanf("%s", t);
ll key = ;
int len = strlen(t);
repu(j, , len) key = key * BASE + id(t[j]);
if(!ma.count(key))
{
printf("Error!");
}
else
{
pair<int, int> tt = ma[key];
repu(j, tt.first, tt.second + ) printf("%c", s[j]);
}
puts("");
}
}
return ;
}
ZOJ 3826的更多相关文章
- ZOJ 3826 Hierarchical Notation 模拟
模拟: 语法的分析 hash一切Key建设规划,对于记录在几个地点的每个节点原始的字符串开始输出. . .. 对每一个询问沿图走就能够了. .. . Hierarchical Notation Tim ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
随机推荐
- Codeforces Round #376 (Div. 2) C. Socks bfs
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- hdu 5587 规律
题意:开始序列{1}; 一次变换{1,1,2}: 两次变换{1,1,2,1,2,2,3} ... 求s[n];题解:打表 S1,S2,S4,S8,S16,S32......公式 S[n]=S[最近的比 ...
- linux 命令行模式下,浏览网页
Ubuntu自带最新版的Gnome桌面,拥有大量的服务和桌面应用程序,让您仅通过一张安装光盘就可以体验到无比舒适的操作环境.下文介绍的在ubuntu下使用终端命令行上网的方法. 第一步,需要安装一个名 ...
- Scrum Meeting---Seven(2015-11-2)
今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 完成了服务器实现 5h 服务器与客户端连接测试 4h 胡亚坤 客户端与服务器端的通信 2h 客户端与 ...
- struts2 if标签示例
下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...
- js中event.keyCode用法及keyCode对照表
HTML 用户名:<input type="text" id="UserAccount" onKeyPress="JumpByEnter(Use ...
- Python内置函数filter, map, reduce
filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当 ...
- Android开发面试经——6.常见面试官提问Android题②(更新中...)
版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http://blog.csdn.net/fi ...
- OpenGL的GLUT初始化函数[转]
OpenGL的GLUT初始化函数 void glutInit(int* argc,char** argv) 初始化GLUT库.对应main函数的形式应是:int main(int argc,char* ...
- JPA基础
目录 目录 1 一.JPA基础 2 1.1 JPA基础 2 1.2JPA开发过程 3 1.3 实体的生命周期及实体管理器常用方法 4 二.环境搭建 5 2.1 添加JPA支持 6 2.2 添加配置文件 ...