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)
上一篇 2022年1月2日 下午8:00
下一篇 2022年1月2日 下午8:00


相关推荐

  • lytro 光场相机 重聚焦

    lytro 光场相机 重聚焦本人刚开始接触机器视觉领域就是学习解压 lytro 光场相机 lytro 光场相机是有 ng 博士创立的 lytro 公司制造的 根据 ng 的论文描述 光场重聚焦主要通过空域和频域傅里叶变换来实现 而且 ng 认为频域的算法复杂度要比空域的要低 但是经过整合 空域的计算时间也是比较满意的 整体上与频域重聚焦相差无几 光场重聚焦实现如下 matlab LF 为五维光场数据 d 为光场变换参数 m n

    2026年3月19日
    2
  • windows平台连接EVE模拟器中网络设备两种方法

    windows平台连接EVE模拟器中网络设备两种方法windows平台下连接eve

    2022年6月7日
    44
  • java jersey使用总结_Java Jersey2使用总结

    java jersey使用总结_Java Jersey2使用总结前言在短信平台一期工作中,为便于移动平台的开发,使用了JavaJersey框架开发RESTFul风格的WebService接口。在使用的过程中发现了一些问题并积累了一些项目经验,做了一下总结,便于个人成长,同时也希望对有需要的同仁有好的借鉴和帮助。简介Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTfulWebservice,它包含三个部分:核心服务器(CoreS…

    2022年7月12日
    16
  • 运行时异常和检查性异常区别

    运行时异常和检查性异常区别运行时异常和检查性异常区别

    2025年11月11日
    5
  • 什么是线程阻塞?为什么会出现线程阻塞?

    什么是线程阻塞?为什么会出现线程阻塞?什么是线程阻塞 在某一时刻某一个线程在运行一段代码的时候 这时候另一个线程也需要运行 但是在运行过程中的那个线程执行完成之前 另一个线程是无法获取到 CPU 执行权的 调用 sleep 方法是进入到睡眠暂停状态 但是 CPU 执行权并没有交出去 而调用 wait 方法则是将 CPU 执行权交给另一个线程 这个时候就会造成线程阻塞 为什么会出现线程阻塞 1 睡眠状态 当一个线程执行代码的时候调用了 slee

    2026年3月18日
    2
  • 多种DLL注入技术原理介绍

    多种DLL注入技术原理介绍本文中我将介绍DLL注入的相关知识。不算太糟的是,DLL注入技术可以被正常软件用来添加/扩展其他程序,调试或逆向工程的功能性;该技术也常被恶意软件以多种方式利用。这意味着从安全角度来说,了解DLL注入的工作原理是十分必要的。不久前在为攻击方测试(目的是为了模拟不同类型的攻击行为)开发定制工具的时候,我编写了这个名为“injectAllTheThings”的小工程的大…

    2022年5月17日
    37

发表回复

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

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