UVALive - 6976 //ZOJ 3826 hash+二分查找

本文介绍了一种名为EON的数据格式,由Marjar大学校长Edward发明,用于通过人类可读的文本传输数据对象。EON格式使用键值对并支持嵌套结构,适合存储和检索复杂的数据结构。文章详细解释了EON的语法,并提供了一个编程挑战,要求读者编写一个解析EON文本并响应查询的程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 di?erent 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 ?rst line of input contains an integer T indicating the number of test cases. For each test case: The ?rst 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 followedby 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 speci?ed. 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

题意:给你一个EON的储存方式对应为{<key>:<vlaue>,<key>:<vlaue>},表示key对应的一个value,其中value可以是一个小的EON,有当前key对应,子的EON用”{}“括起来,表示是一个整体然后q次询问给你一个key或者可以中key求vlaue,没有的话就输出Error。

思路:题意很简单,只是怎么做,最头疼的就是这些key中key了,我想的就是dfs把每一个{}中的所有key和vlaue都预处理出来,每一个{}都是一个节点now,吧key用hash对应出来,如果value不是一个新的EON的话就压入当的一个vector[now]里,是一个新的EON的话进入下一个节点dfs,然后遍历下一个key,遇到了}就retrun。查询的时候同样查找key的hash值(二分),如果没有的话就是错误,还有子EON的key的话话就进入下一个EON,还有子EON输出就好。模拟题,具体看代码吧

代码:

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=3e5+5;
const int inf=0x3f3f3f3f;
const int base=137;
const ll linf=8e18+9e17;
const int mod=1e9+97;
const double e=exp(1.0);
const double pi=acos(-1);
const double eps=1e-6;
char s[maxn],ss[maxn];
int l,cnt,pos;
struct Hash{
	ll key;
	string vla;
	int next,flag;
	bool operator < (const Hash &t)const{
	return key<t.key;
	}
	bool operator == (const Hash &t)const{
	return key==t.key;
	}
};
vector<Hash>v[maxn];
map<string,int>mp;
void dfs()
{
	int now=cnt++;
	ll vla1=0,vla2=0;
	string str;
	int f=1;
	while(pos<l)
	{
		pos++;
		if(s[pos]=='"'){
		continue;}
		if(s[pos]==':'){
		f=2;
		continue;
		}
		if(s[pos]==','||s[pos]=='}')
		{
		if(s[pos-1]=='"')v[now].push_back((Hash){vla1,str,-1,0});
		vla1=0,vla2=0,f=1;
		str.clear();
		if(s[pos]=='}')return ;	
		continue;
		}
		
		if(s[pos]=='{'){
			v[now].push_back((Hash){vla1," ",cnt,pos});
			vla1=0;
			f=1;
			dfs();
			continue;
			}
			
		if(f==1)vla1=(vla1*base+s[pos])%mod;
		if(f==2)vla2=(vla2*base+s[pos])%mod,str+=s[pos];
	}
}
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",s);
		l=strlen(s);
		cnt=0,pos=0;
		dfs();
		for(int i=0;i<cnt;i++)
		sort(v[i].begin(),v[i].end());
		scanf("%d",&n);
		while(n--)
		{
			scanf("%s",ss);
			l=strlen(ss);
			int now=0;
			ll vla=0;
			for(int i=0;i<=l;i++)
			{
				if(ss[i]=='"')continue;
				if(ss[i]=='.'||i==l){
					int f=lower_bound(v[now].begin(),v[now].end(),(Hash){vla,"",1,0})-v[now].begin();
					if(v[now][f].key!=vla){
						puts("Error!");
					}
					else if(i==l&&v[now][f].next!=-1){
					int j=v[now][f].flag,num=0;
					while(1)
					{
						putchar(s[j]);
						if(s[j]=='{')num++;
						if(s[j]=='}')num--;
						if(num==0)break;
						j++;
					}
					puts("");
					}
					else {
						if(v[now][f].next!=-1)now=v[now][f].next;
						else putchar('"'),cout<<v[now][f].vla,putchar('"'),puts("");
						vla=0;
					}
					continue;
				}
				vla=(vla*base+ss[i])%mod;
			}
		}
		for(int i=0;i<cnt;i++)v[i].clear();
	}
	return 0;
}
/*
1
{"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"},"stu1":{"stu01":"Alice","stu02":"Bob"}}
4 
"hm"
"stu1"
"stu"."stu01"
"students"
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值