标签:完成 sizeof tps mes cpp 要求 its eof query
要求写一个小程序完成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\n", 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\n", node[x].val);
else
printf("OBJECT\n");
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("%[^\n]", str);
getchar();
for (int i = 0; str[i] != '\0'; 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] = '\0';
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] != '\0'; j++);
for (; i < j; i++)
{
query[queCnt][tmp++] = str[i];
}
query[queCnt++][tmp] = '\0';
if (str[j] == '\0')
break;
}
if (!dfs(0, 0))
printf("NOTEXIST\n");
}
return 0;
}
标签:完成 sizeof tps mes cpp 要求 its eof query
原文地址:https://www.cnblogs.com/acboyty/p/11369478.html