ZOJ 3826 Hierarchical Notation 模拟
模拟: 语法的分析
hash一切Key建设规划,对于记录在几个地点的每个节点原始的字符串开始输出。
。
。。
对每一个询问沿图走就能够了。
。。
。
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack> using namespace std; typedef long long int LL; LL hash(char* str)
{
LL ret=0;
int len=strlen(str);
for(int i=0;i<len;i++)
{
ret=ret*123+(LL)(str[i]-'0');
}
return ret;
} map<LL,int> mp;
stack<int> stk; char str[400007],que[400007];
bool graph[10000][10000];
int stpt[400007]; void init()
{
mp.clear(); while(!stk.empty()) stk.pop();
memset(graph,false,sizeof(graph));
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
init();
scanf("%s",str);
int len=strlen(str);
int word=0;
bool readname=false;
char name[50]; int na;
for(int i=0;i<len;i++)
{
if(str[i]=='{') stk.push(word);
else if(str[i]=='}') stk.pop();
else if(str[i]=='"')
{
if(readname==false)
{
readname=true;
memset(name,0,sizeof(name)); na=0;
}
else if(readname==true)
{
readname=false;
LL id=hash(name);
word++;
mp[id]=word;
stpt[mp[id]]=i+1;
graph[stk.top()][word]=true;
}
}
else if(str[i]==':')
{
if(str[i+1]=='{') continue;
else if(str[i+1]=='"')
{
i++;
while(str[i+1]!='"')
i++;
i++;
}
}
else if(readname==true)
{
name[na++]=str[i];
}
}
int m;
scanf("%d",&m);
while(m--)
{
scanf("%s",que);
int len2=strlen(que);
bool flag=true;
na=0; int p=0;
readname=false;
for(int i=0;i<len2&&flag;i++)
{
if(que[i]=='"')
{
if(readname==false)
{
na=0; memset(name,0,sizeof(name));
readname=true;
}
else if(readname==true)
{
readname=false;
LL id=hash(name);
if(graph[p][mp[id]]==true)
{
p=mp[id];
}
else flag=false;
}
}
else if(que[i]=='.') continue;
else name[na++]=que[i];
}
if(flag==false) puts("Error!");
else
{
char cc=str[stpt[p]+1];
int dep=0;
if(cc=='{') cc='}';
int ii=stpt[p]+1;
if(cc=='"') { ii++; putchar('"'); }
while(true)
{
if(cc=='}')
{
if(str[ii]=='{') dep++;
if(str[ii]=='}') dep--;
if(dep==0) break;
}
else if(cc=='"')
{
if(str[ii]=='"') break;
}
putchar(str[ii]); ii++;
}
putchar(cc);
putchar(10);
}
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
ZOJ 3826 Hierarchical Notation 模拟的更多相关文章
- ZOJ - 3829 Known Notation(模拟+贪心)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...
- zoj3826 Hierarchical Notation (字符串模拟)
Hierarchical Notation Time Limit: 2 Seconds Memory Limit: 131072 KB In Marjar University, stude ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- ZOJ 3826
Hierarchical Notation Time Limit: 2 Seconds Memory Limit: 131072 KB In Marjar University, stude ...
- zoj 3829 Known Notation
作者:jostree 转载请说明出处 http://www.cnblogs.com/jostree/p/4020792.html 题目链接: zoj 3829 Known Notation 使用贪心+ ...
- ZOJ - 3930 Dice Notation 【模拟】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3930 题意 给出一串字符串 如果是 '+' '-' '*' '/ ...
- Dice Notation(模拟)
Dice Notation Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- ZOJ 3829 Known Notation 贪心
Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
随机推荐
- Visual Leak Detector(vld)无法显示内存泄露文件名称与行号
使用VLD測有没内存泄露的时候,出现(File and line number not available): (Function name unavailable) 查看VS控制台,发现 已载入&q ...
- mysql 服务启动报1607 error
[问题说明] mysql曾经还是好好的,突然就不行了...不知道是否使用了腾讯C盘搬家工具引起的... watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2h ...
- 4、深入理解Bean
本节知识点: 1. Bean 的自己主动装配(了解) 2. bean 之间的关系:继承:依赖 3.Bean 的作用域:能够在 <bean> 元素的 scope 属性里设置 Bean 的作用 ...
- 《windows核心编程系列》十八谈谈windows钩子
windows应用程序是基于消息驱动的.各种应用程序对各种消息作出响应从而实现各种功能. windows钩子是windows消息处理机制的一个监视点,通过安装钩子能够达到监视指定窗体某种类型的消息的功 ...
- 约瑟夫问题--list模拟循环链表
约瑟夫问题 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 n个人想玩残酷的死亡游戏,游戏规则例如以下: n个人进行编号,分别 ...
- c++野指针 之 实战篇
一:今天做poj上的3750那个题,用到了list的erase方法.提交之后总是报runtime error! 纠结了好长时间.曾有一度怀疑过vector的erase和list的erase处理方式不一 ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第二步---编辑器(1)--触摸加入点
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦,他说:随便写,第一别全然照搬代码:第二能够说 ...
- 使用SSIS对Dynamics CRM 系统进行数据迁移
嗨,各位.近期项目一直都非常忙,而且自己也一直在思考职业发展的问题,所以有非常长一段时间没静下心写几篇Blog了.近期我參与的项目是Dynamics CRM 2011 到 Dynamics CRM 2 ...
- XP下採用DirectShow採集摄像头
转载请标明是引用于 http://blog.csdn.net/chenyujing1234 欢迎大家提出意见,一起讨论! 须要演示样例源代码的请独自联系我. 前提: 摄像头能正常工作.摄像头有创建di ...
- 辛星解读为什么PHP须要模板
近期有个人问我:为什么PHP须要模板呢?整个站点的编写都是我一个人完毕的,从前端到后端,都是这样,我一个人写站点是不是就不须要模板了呢?我当时还真给问住了,也没想好非常合适的回答它的方式,于是就随便说 ...