ZOJ 3826 Hierarchical Notation 模拟

ZOJ 3826 Hierarchical Notation 模拟

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

模拟: 语法的分析

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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117623.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • UART接口控制器

    UART接口控制器主设备与从设备通过总线来进行数据通信,是一个数字系统不可或缺的一部分,本篇讲述一种常见的总线控制器UART串行数据接口,也称为串口。串口的标准一般有,RS-232、RS-422与RS-485标准,我们讲述的是RS-232接口信号。1、接口信号定义RS-232最常见的是9脚接口表1-1:RS-232接口定义在实际的应用中,我们只需要关注两个接口,数据接收(RXD)和数据发送(TXD),而…

    2025年11月16日
    4
  • goland调试go代码_debug运行

    goland调试go代码_debug运行如何使用dlv结合Goland进行程序debug调试相信很多Golang的初级玩家不会进行程序的Debug定位问题单纯的靠脑子,或者效率很低的不断的添加日志打印,别问我为什么知道的因为我就是这样的,最好最快捷的问题定位方式一定是使用Debug打断点调试,这时就引出了本文的主角dlv。实际上,delve才是全称,dlv只是启动命令,如果VScode,Goland,默认使用的调试器就是基于delve的。安装dlv参考官方的安装方法,把dlv命令安装在go.

    2025年6月14日
    4
  • 尚硅谷java培训几个月,经验分享

    尚硅谷java培训几个月,经验分享Java代码是怎么运行的?Java的基本类型Java虚拟机是如何加载Java类的JVM是如何执行方法调用的?(上)JVM是如何执行方法调用的?(下)JVM是如何处理异常的?JVM是如何实现反射的?JVM是怎么实现invokedynamic的?(上)JVM是怎么实现invokedynamic的?(下)Java对象的内存布局垃圾回收(上)垃圾回收(下)Java内存模型Java虚拟机是怎么实现synchronized的?Java语法糖与Java编译器16

    2022年7月8日
    21
  • ftp服务器软件 性能对比,常用ftp服务器软件介绍[通俗易懂]

    ftp服务器软件 性能对比,常用ftp服务器软件介绍[通俗易懂]导读:对于服务器远程文件的管理,最常见的就是使用ftp服务器软件进行管理,上传下载文件等操作,可以轻松实现本地上传文件到服务器,以及从服务器下载文件到本地,快捷方便简单,接下来我们重点介绍几款比较好用的ftp服务器软件,供大家参考,下面介绍的是在win系……对于服务器远程文件的管理,最常见的就是使用ftp服务器软件进行管理,上传下载文件等操作,可以轻松实现本地上传文件到服务器,以及从服务器下载文件…

    2025年10月27日
    6
  • 视频全面解读PHP面试[通俗易懂]

    视频全面解读PHP面试

    2022年2月9日
    450
  • Java免费的开发工具有哪些?分享这15个!

    Java免费的开发工具有哪些?分享这15个!随着Java行业需求增加,Java工程师岗位薪资节节升高,很多小伙伴想要通过快速的方式掌握Java技能。对于初学Java的小伙伴来说了解一些免费的Java开发工具让我们工作、学习更顺畅,那么免费Java开发工具有哪些?针对这点我汇总了一些,可供参考。1、Java免费开发工具:JDK(Java开发工具包)如果你打算用Java开发一些小程序和应用程序,那么首先得给自己准备一个类似于JDK的工具,…

    2022年7月7日
    162

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号