1. 程式人生 > >UVALive - 6976 //ZOJ 3826 hash+二分查詢

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

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"
*/