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! 题意: 牡丹江的H题, 写一个很类似python的字典的东西,然后给出键值输出对应的值,可以嵌套。ps 好喜欢python这样的自由、简洁,打完这星期国赛就专研一下python 思路: 使劲模拟一遍就ok啦,一开始tle了,看了别人题解,原来应该hash一下字符串再存入map中的,改hash后150ms就过了
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
const int N = ; char s[N],query[N];
int p,sz,qsz; struct _node{
int val_l,val_r;
map<string,int> mp;
void clear()
{
val_l=val_r=;
mp.clear();
}
}node[N];
int cnt; void readname(char *s,int sz,int &l,int &r)
{
l=p++;
while(p<sz && s[p]!='"')
p++;
r=p++;
// cout<<l<<' '<<r<<' '<<s[l]<<' '<<s[r]<<endl;
} void prints(int l,int r)
{
while(l<=r && l<sz) putchar(s[l++]);
puts("");
} void build(int root)
{
node[root].val_l=p++;
if(s[p]=='}') goto bk;
while(p<sz)
{
int l,r;
readname(s,sz,l,r);
string key(s,l,r-l+);
// cout<<l<<' '<<r<<' '<<root<<' '<<key<<endl;
node[root].mp[key]=cnt;
// cout<<node[root].mp[key]<<endl;
node[cnt++].clear();
p++; // :
if(s[p]=='{')
build(cnt-);
else
readname(s,sz,node[cnt-].val_l,node[cnt-].val_r);
if(s[p]=='}') break;
else p++; // ,
}
bk:
node[root].val_r=p++;
} void getans(int root,int &ansl,int &ansr)
{
int l,r;
while(true)
{
readname(query,qsz,l,r);
string str(query,l,r-l+);
if(node[root].mp.find(str)!=node[root].mp.end())
root=node[root].mp[str];
else
{
root=-;
break;
}
if(query[p]!='.') break;
p++;
}
if(root==-) ansl=ansr=-;
else ansl=node[root].val_l,ansr=node[root].val_r;
} void run()
{
// scanf("%s",s);
gets(s);
p=,sz=strlen(s);
node[].clear();
cnt=;
build();
int q;char c;
scanf("%d",&q);
scanf("%c",&c);
while(q--)
{
// scanf("%s",query);
gets(query);
qsz=strlen(query);
p=;
int l,r;
getans(,l,r);
if(l==-)
puts("Error!");
else
prints(l,r);
}
// cout<<endl;
// for(map<string,int>::iterator it=node[0].mp.begin(); it!=node[0].mp.end(); it++)
// cout<<it->first<<' '<<it->second<<endl;
} void test()
{
scanf("%s",s);
p=;
prints(,);
} int main()
{
#ifdef LOCAL
freopen("in","r",stdin);
#endif
// test();
int _;char c;
scanf("%d",&_);
scanf("%c",&c);
while(_--)
run();
return ;
}

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

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

#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm> using namespace std;
typedef unsigned long long ll;
typedef pair<int,int> pii; const int maxn = ;
const ll x = ; int N, Q, mv;
char op[maxn], s[maxn];
map<ll, pii> G; inline int idx(char ch) {
if (ch >= '' && ch <= '')
return ch - '';
else if (ch >= 'A' && ch <= 'Z')
return ch - 'A' + ;
else if (ch >= 'a' && ch <= 'z')
return ch - 'a' + ;
else if (ch == '.')
return ;
return ;
} void solve (ll u) {
ll tmp = u; while (s[mv] != '}') {
mv++;
if (s[mv] == '}')
return;
u = tmp; while (s[mv] != ':')
u = u * x + idx(s[mv++]); int l = ++mv; if (s[mv] == '{')
solve(u * x + 62LL);
else
while (s[mv+] != ',' && s[mv+] != '}') mv++; G[u] = make_pair(l, mv);
mv++;
}
} int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%s", s); mv = ;
G.clear();
solve(); scanf("%d", &Q);
for (int i = ; i <= Q; i++) {
scanf("%s", op); ll ret = ;
int len = strlen(op); for (int i = ; i < len; i++)
ret = ret * x + idx(op[i]); if (G.count(ret)) {
pii u = G[ret];
for (int i = u.first; i <= u.second; i++)
printf("%c", s[i]);
printf("\n");
} else
printf("Error!\n");
}
}
return ;
}

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. python 基础 9.2 mysql 事务

    一. mysql 事务    MySQL 事务主要用于处理操作量大,复杂度高的数据.比如,你操作一个数据库,公司的一个员工离职了,你要在数据库中删除它的资料,也要删除该人员相关的,比如邮箱,个人资产等 ...

  2. 常用脚本--查看死锁和阻塞usp_who_lock(转)

    USE [master] GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 02/07/2014 11:51:24 ...

  3. 微信小程序设计指南

    微信小程序设计指南 · 小程序 https://developers.weixin.qq.com/miniprogram/design/index.html

  4. Cocos2d-X开发中国象棋《九》走棋规则

    在上一节中实现了走棋,这篇博客将介绍中国象棋中的走棋规则 在写博客前先可能一下象棋的走棋规则: 1)将 将的坐标关系:横坐标相等,纵坐标相减绝对值等于1,或者纵坐标相等,横坐标相减绝对值等于1 将的特 ...

  5. Other Linker flags 添加 -Objc导致包冲突

    Other Linker flags 添加 -Objc导致包冲突 先尝试不添加-Objc,不行的话尝试下面的方法. 第三方冲突解决办法: https://www.jianshu.com/p/02846 ...

  6. 【Java 语言生态篇】Junit 测试单元

    01 概述   JUnit是一个由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework).Junit测试是白盒测试.JUn ...

  7. debian下编译openwrt固件

    参考文章:Ubuntu下编译OpenWRT固件 我买的路由器是RG100A-AA,采用了bcm63xx系列的芯片. 下载openwrt源码: svn co svn://svn.openwrt.org/ ...

  8. HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP

    题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others)  ...

  9. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  10. Codeforces 854B Maxim Buys an Apartment:贪心

    题目链接:http://codeforces.com/contest/854/problem/B 题意: 有n栋房子从1到n排成一排,有k栋房子已经被售出. 现在你要买一栋“好房子”. 一栋房子是“好 ...