ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模拟)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5380

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

题意:

模拟python的字典。给出一个字典,然后有1000个查询,输出对应的键值。

分析:

简单粗暴的字符串神烦模拟。因为打的网络同步赛,内存限制得很死,采用了离线操作。

抓住这几个关键的符号:‘{’ ‘}‘ ‘,‘ ‘:‘ 进行操作。具体见代码及注释,数据不是非常恶心。

#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;

#define maxn 400007

char buff[maxn];

map<string,vector<int> > MAP;

char buf[maxn];

void print(int pos)
{
    int rest=0;
    while ((buff[pos]!=','&& buff[pos]!='}' || rest>0) && buff[pos]!='\0')
    {
        if (buff[pos]=='{')
            rest++;
        if (buff[pos]=='}')
            rest--;

        putchar(buff[pos++]);
    }
    putchar('\n');
}

int ans[1007];

void analyse(const char *s)
{
    int j=0;

    for (int i=0;s[i]!='\0';i++)
    {
        if (s[i]=='{')
        {
            while (j>0 && buf[j-1]!='|') buf[j--]='\0'; // 先往前删到键
            if (j>0 && buf[j-1]=='|') j--;
            buf[j]='\0';
            buf[j++]='.';
            buf[j]='\0';
            continue;
        }

        if (s[i]=='}')  //删掉两层
        {
            while (j>0 &&buf[j-1]!='.') buf[j--]='\0';
            buf[j--]='\0';
            while (j>0 &&buf[j-1]!='.') buf[j--]='\0';
            buf[j]='\0';

            continue;
        }
        if (s[i]==':')
        {
            buf[j]='\0';
            if (MAP.count(string(buf))==1)  //如果是要查询的,把输出位置记录
            {
                for (int k=0;k<MAP[(string)buf].size();k++)
                    ans[MAP[(string)buf][k]]=i+1;
            }
            buf[j++]='|'; //'|'用来分割
            buf[j]='\0';
            continue;
        }
        if (s[i]==',')
        {
            while (buf[j-1]!='.') buf[j--]='\0';  //删掉一层

            continue;
        }
        buf[j++]=s[i];
        buf[j]='\0';
    }
}

char query[maxn];

int main()
{
    query[0]='.';
    int T_T;
    scanf("%d",&T_T);

    while (T_T--)
    {
        MAP.clear();
        scanf("%s",buff);

        int q;
        scanf("%d",&q);

        memset(ans,-1,sizeof ans);

        for (int i=1;i<=q;i++)
        {
            scanf("%s",query+1);
            MAP[string(query)].push_back(i);
        }

        analyse(buff);

        for (int i=1;i<=q;i++)
            if (ans[i]==-1)
                puts("Error!");
            else
                print(ans[i]);
    }

    return 0;

}
时间: 2024-10-06 10:14:22

ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模拟)的相关文章

ZOJ 3829 Known Notation (2014牡丹江H题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. I

zoj 3826 Hierarchical Notation(模拟)

题目链接:zoj 3826 Hierarchical Notation 题目大意:给定一些结构体.结构体有value值和key值,Q次询问,输出每一个key值相应的value值. 解题思路:思路非常easy.写个类词法的递归函数,每次将key值映射成一个hash值,用map映射每一个key的value起始终止位置,预处理完了查询就非常easy了. 这题是最后10分钟出的.由于没有考虑value为{}的情况,导致RE了.可是zoj上显示的是SE,表示不理解什么意思,事实上就是RE.只是另一个地方会

zoj 3829 Known Notation(2014年牡丹江区域赛k题)

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre

ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns. Every day after work, Edward will place

zoj 3822 Domination(2014牡丹江区域赛D题)

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

2014牡丹江现场-H-大模拟

大模拟 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <string> 5 #include <vector> 6 #include <map> 7 8 using namespace std; 9 10 const int maxn = 1e7+10; 11 const int max_node = 1e5+10; 12 13 i

2014牡丹江——Hierarchical Notation

题目链接 字符串模拟 const int MAXN = 2000000; char ipt[MAXN], t[MAXN]; int f[MAXN], len, to[MAXN]; map<string, string> mp[MAXN]; string x, key, ans; string i2s(int n) { string ret = ""; if (n == 0) ret += '0'; else { while (n) { ret += n % 10 + '0'

zoj3826 Hierarchical Notation (字符串模拟)

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 trans

2014牡丹江区域赛H(字典树)ZOJ3826

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 trans