您的位置:首页 > 其它

zoj-3826(字符串模拟)

2017-11-01 15:03 381 查看
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
4000
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!


题意:给出的是一段字符,前面是键,冒号后面是值,而用花括号括起来的都是算在前面的键的值。问给出一个键求它对应的值。若找到则输出,没有找到输出Error!

思路:题目的冒号在10000个以内,键和值长度不超过20,所以加起来字符串长度不会超过400000。
这题因为冒号后可能会接上一个花括号,所以会有层的概念,就应该用一个递归函数。而对应输入的字符串要返回值,可以用map来存储这些内容。用哈希的办法将这些键以独一无二的数来存储。map记录着值的在字符串输出的开始位置和结束位置。

哈希时用u=u*x+hashkey(s[mv]);这里的x只要是大于规定的字符对应的最大数就行了,这个代码中规定的字符最大是63所以x只要取64就行了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int maxn=400005;
const int x=64;
typedef long long ll;
map<ll ,pair<int,int> > mapp;
char s[maxn],p[maxn];
int mv,t,n;
int hashkey(char t)
{
if(t>='0'&&t<='9')
return t-'0';
else if(t>='A'&&t<='Z')
return t-'A'+10;
else if(t>='a'&&t<='z')
return t-'a'+36;
else if(t=='.')
return 62;
else
return 63;
}
void insertt(ll u)
{
ll tmp=u;
while(s[mv]!='}')
{
mv++;
if(s[mv]=='}')//递归出口
return;
u=tmp;
while(s[mv]!=':')
{
u=u*x+hashkey(s[mv]);
mv++;
}
int l=++mv;
if(s[mv]=='{')
insertt(u*x+62);//这里为了是对应输入给的
else
while(s[mv+1]!=','&&s[mv+1]!='}') mv++;
mapp[u]=make_pair(l,mv);
mv++;
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
mv=0;
mapp.clear();
insertt(0);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",p);
ll u=0;
for(int i=0;i<strlen(p);i++)
u=u*x+hashkey(p[i]);
if(mapp.count(u))
{
for(int i=mapp[u].first;i<=mapp[u].second;i++)
printf("%c",s[i]);
printf("\n");
}
else
printf("Error!\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: