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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java 用户态_深入理解内核态和用户态

    java 用户态_深入理解内核态和用户态1.内核态和用户态、内核线程和用户线程等解释操作系统调度CPU的最小单元是线程,也叫轻量级进程(LightWeightProcess),在一个进程里可以创建多个线程,这些线程都拥有各自的计数器、堆栈和局部变量等属性,并且能够访问共享的内存变量。处理器在这些线程上高速切换,让使用者感觉到这些线程在同时执行。系统的用户空间和内核空间:虚拟内存被操作系统划分成两块:内核空间和用户空间,内核空间是内…

    2025年12月12日
    5
  • 矩阵论(1)三种常见的矩阵范数

    矩阵论(1)三种常见的矩阵范数总结了三种矩阵范数:1-范数,2-范数以及∞-范数。

    2026年1月25日
    7
  • 测试用例的设计方法:等价类划分法[通俗易懂]

    测试用例的设计方法:等价类划分法[通俗易懂]欢迎大家关注我的个人公众号:小朱谈软件测试,全是干货哦。测试用例的设计方法对测试人员来说,非常重要,在工作中,确实会用到。测试用例设计的越全面,线上问题肯定越少,合理地运用这些设计方法,可以减少冗余用例,提高测试效率和覆盖率。常见测试用例的设计方法有八种,下图中标红的是需要掌握的。优先级从等价类划分法,边界值分析法方向排序。每种设计方法,我都会分析,今天我们先一起看看什么是等价类划分法。等价类划分法:官方定义:将程序所有可能的输入数据…

    2022年10月18日
    4
  • bookkeeper具体做什么_reading for knowledge翻译

    bookkeeper具体做什么_reading for knowledge翻译序言,关于capCAP是分布式系统中的一个特别重要的理论。CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性)、Availability(可用性)、Partitiontolerance(分区容错性),三者不可得兼。CAP是NOSQL数据库的基石。分布式系统的CAP理论:理论首先把分布式系统中的三个特性进行了如下归纳:一致性(C):在分布式系统中的所…

    2025年7月7日
    2
  • 数据库四大特性_Mysql数据库四种特性

    数据库四大特性_Mysql数据库四种特性1、原子性(Atomicity):原子性是指事务是一个不可分割的工作单位,事务中的操作要么全部成功,要么全部失败。比如在同一个事务中的SQL语句,要么全部执行成功,要么全部执行失败。2、一致性(Co

    2022年8月4日
    10
  • 小白勿进!安卓版java游戏盒下载「建议收藏」

    小白勿进!安卓版java游戏盒下载「建议收藏」为什么要分库分表?首先回答一下为什么要分库分表,答案很简单:数据库出现性能瓶颈。用大白话来说就是数据库快扛不住了。数据库出现性能瓶颈,对外表现有几个方面:大量请求阻塞在高并发场景下,大量请求都需要操作数据库,导致连接数不够了,请求处于阻塞状态。SQL操作变慢如果数据库中存在一张上亿数据量的表,一条SQL没有命中索引会全表扫描,这个查询耗时会非常久。存储出现问题业务量剧增,单库数据量越来越大,给存储造成巨大压力。从机器的角度看,性能瓶颈无非就是CPU、内存、磁盘、网络这些,要解决性能瓶颈

    2022年7月8日
    29

发表回复

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

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