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 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
随机推荐
- firefox 安装flash插件
第一步下载并解压flash解压出一个文件:libflashplayer.so 和文件夹usr 第二步查找plugins其他程序或许也有这个文件夹所以先查找firefox在查找mozilla看在哪个文件 ...
- CUBRID学习笔记 14 删除主键错误
发生这样的问题.其实和别的数据库基本原因差不多. 就是外键冲突. 看看有没有外键引用这个表的主键. 然后删除外键. 就可以了 SELECT class_name FROM db_index WHER ...
- So easy Webservice 3.使用HttpClient工具调用Webservice接口
首先,看看webservice服务调用演示: a) 登录http://www.webxml.com.cn b) 单击手机查询服务 c) 选择要调用的方法 例如: getMobileCodeInfo 输 ...
- 3----lua的数据转换及运算符
lua的基本数据类型转换 转换成字符串 tostring( ... ) 可以将布尔类型和数字类型的值转换为字符串类型的值 local num=1; print(type(num)) newNum = ...
- Python基础学习笔记(九)常用数据类型转换函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...
- 转 cocos2dx内存优化 (之二)
一.cocos2dx之如何优化内存使用(高级篇) 本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=93 一.内存优化原则 为了优化应用内存,你应该知道 ...
- mybatis的xml文件中如何处理大小于号
在mybatis的xml配置文件中会遇到大小于号转化的问题,解决问题的方法如下: 1.用转义字符把>和<替换掉 SELECT * FROM test WHERE AND start_dat ...
- Spring读书笔记-----使用Spring容器(二)
一.使用ApplicationContext 前面介绍了,我们一般不会使用BeanFactory实例作为Spring容器,而是使用ApplicationContext实例作为容器,它增强了BeanFa ...
- Android_安装GooglePlay
百度搜索:“google play 安装” http://jingyan.baidu.com/article/cbf0e500f4645b2eab28935a.html http://samsungb ...
- C++—复合类型
内容概要: -创建和使用数组 -创建和使用C-风格字符串 -创建和使用string类字符串 -使用方法getline()和get()读取字符串 -混合输入字符串和数字 -创建和使用结构 -创建和使用共 ...