zoukankan      html  css  js  c++  java
  • ZOJ 3826

    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

    赛后看见别人补这个题,我便也补,其实场上没有看这个题,听说是到模拟,我觉得正解应该就是字符串处理来模拟数据库,然而这个题可以更简单,由于数据少,10000条key,所以hush便出现重的几率很小,看见大家都是字符串+hush水的,我也这么做的,总之,这样做简单啊。。。

    注意边界处理

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define repd(i, a, b) for(int i = b; i >= a; i--)
    #define sfi(n) scanf("%d", &n)
    #define sfl(n) scanf("%lld", &n)
    #define pfi(n) printf("%d
    ", n)
    #define pfl(n) printf("%lld
    ", n)
    #define MAXN 1000005
    #define BASE 131
    char s[MAXN];
    map<ll, pair<int, int> > ma;
    ll id(char c)
    {
        return ((ll)c) % BASE;
    }
    int p;
    void Create(ll f)
    {
        while(s[p] != '}')
        {
            if(s[++p] == '}') return ;
            ll t = f;
            while(s[p] != ':')
            {
                t = t * BASE + id(s[p]);
                p++;
            }
            int st = ++p;
            if(s[p] == '{')
            {
                Create(t * BASE + id('.'));
            }
            else
            {
                while(s[p] != ',' && s[p] != '}') p++;
            }
            if(s[p] == '}')
            ma[t] = pair<int, int>(st, p + 1);
            else ma[t] = pair<int, int>(st, p);
            //cout<<t<<endl;
        }
        return ;
    }
    
    int main()
    {
        int T;
        sfi(T);
        while(T--)
        {
            ma.clear();
            scanf("%s", s);
            int n;
            char t[MAXN];
            p = 0;
            Create(0);
            sfi(n);
            repu(i, 0, n)
            {
               scanf("%s", t);
               ll key = 0;
               int len = strlen(t);
               repu(j, 0, len) key = key * BASE + id(t[j]);
               if(!ma.count(key))
               {
                   printf("Error!");
               }
               else
               {
                   pair<int, int> tt = ma[key];
                   repu(j, tt.first, tt.second + 1) printf("%c", s[j]);
               }
               puts("");
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    前端页面性能参数搜集
    前端性能优化的另一种方式——HTTP2.0
    邮件服务器Postfix的管理 重启php-fpm
    腾讯云域名注册服务
    centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课
    atime、mtime、ctime的区别及如何降低atime更新 mount时的option noatime
    centos LNMP第一部分环境搭建 LAMP LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/php/{p.conf.default,p.conf} php运行方式SAPI介绍 第二十三节课
    centos LAMP第四部分mysql操作 忘记root密码 skip-innodb 配置慢查询日志 mysql常用操作 mysql常用操作 mysql备份与恢复 第二十二节课
    centos LAMP第三部分php,mysql配置 php配置文件 配置php的error_log 配置php的open_basedir 安装php的扩展模块 phpize mysql配置第二十一节课
    LVM的一些问题汇总 tune2fs命令
  • 原文地址:https://www.cnblogs.com/sunus/p/4696636.html
Copyright © 2011-2022 走看看