• CCF-CSP题解 201709-3 JSON查询


    要求写一个小程序完成JSON查询的功能。

    查询dfs就好了。

    存储JSON对象用图(树)就好,把(<key[],type,val[]>)作为节点,然后又是字符串处理了。

    其实就是个简化版的201809-3元素选择器

    虽然说80%的评测用例对象层数不超过2层,但是经测试,凉心出题人评测用例最多的层数是20层。

    #include <bits/stdc++.h>
    const int maxn = 100;
    
    using namespace std;
    
    struct tNode
    {
        char key[85];
        int type; // 0 STRING 1 OBJECT
        char val[85];
    };
    tNode node[maxn*10+5];
    
    int to[maxn*10+5];
    int nex[maxn*10+5];
    int head[maxn*10+5], cnt = 0;
    
    void addedge(int a, int b)
    {
        to[cnt] = b; nex[cnt] = head[a];
        head[a] = cnt++;
    }
    
    char query[50][85];
    int queCnt;
    
    bool dfs(int x, int y)
    {
        // printf("%s %s
    ", node[x].key, query[y]);
        if (strcmp(node[x].key, query[y]) == 0)
        {
            if (y == queCnt - 1)
            {
                if (node[x].type == 0)
                    printf("STRING %s
    ", node[x].val);
                else
                    printf("OBJECT
    ");
                return true;
            }
            else
            {
                for (int i = head[x]; i != -1; i = nex[i])
                {
                    if (dfs(to[i], y + 1))
                        return true;
                }
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    
    int main()
    {
        int n, m;
        scanf("%d%d", &n, &m);
        getchar();
    
        memset(head, -1, sizeof(head));
        stack<int> sta;
        int num = 0;
        char key[85] = "", val[85] = "";
        while (n--)
        {
            char str[85];
            scanf("%[^
    ]", str);
            getchar();
            for (int i = 0; str[i] != ''; i++)
            {
                if (str[i] == '{')
                {
                    strcpy(node[num].key, key);
                    node[num].type = 1;
                    if (!sta.empty())
                        addedge(sta.top(), num);
                    sta.push(num++);
                    strcpy(key, "");
                }
                else if (str[i] == '"')
                {
                    char temp[85], tempCnt = 0;
                    for (i = i + 1; str[i] != '"'; i++)
                    {
                        if (str[i] == '\')
                            temp[tempCnt++] = str[++i];
                        else
                            temp[tempCnt++] = str[i];
                    }
                    temp[tempCnt] = '';
                    if (strcmp(key, "") == 0)
                        strcpy(key, temp);
                    else
                    {
                        strcpy(val, temp);
                        strcpy(node[num].key, key);
                        strcpy(node[num].val, val);
                        node[num].type = 0;
                        addedge(sta.top(), num++);
                        strcpy(key, "");
                    }
                }
                else if (str[i] == '}')
                {
                    sta.pop();
                }
            }
        }
    
        while (m--)
        {
            char str[85];
            scanf("%s", str);
            queCnt = 0;
            strcpy(query[queCnt++], "");
            for (int i = 0; ; i++)
            {
                int tmp = 0;
                int j = i + 1;
                for (; str[j] != '.' && str[j] != ''; j++);
                for (; i < j; i++)
                {
                    query[queCnt][tmp++] = str[i];
                }
                query[queCnt++][tmp] = '';
                if (str[j] == '')
                    break;
            }
            if (!dfs(0, 0))
                printf("NOTEXIST
    ");
        }
    
        return 0;
    }
    
  • 相关阅读:
    Linux内核之系统调用
    [转]Linux最多支持的SCSI Disk的数目
    bash一些技巧
    [转]Iptables 规则 一些简单实例和详细介绍
    近来工作和面试一些人的感受(原)
    自动给URL地址加上超链接
    在ASP.NET虚拟主机上实现URL重写(UrlRewrite)
    使用Office组件读取Excel,引用Microsoft.Office.Interop.Excel出现的问题
    Fixing Twitter 提高世界上最流行的微博客系统性能和可扩展性(转)
    从优酷网学习SEO的标题(title)和关键字(keywords)如何选择
  • 原文地址:https://www.cnblogs.com/acboyty/p/11369478.html
Copyright © 2020-2023  润新知