ZOJ 3826(字符串hash)
2015-08-01 09:55
344 查看
本体的意思是给了一个{ key:value , key:value ..... }的串,且value的形式可以也是这个形式(即是递归的),那么给定一些key1.key2.key3...类似如此的键值查询,让输出对饮键下的value,(没有该键值输出Error!)
example:
输入 :
{"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"}}
4
"hm"
"stu"
"stu"."stu01"
"students"
输出为:
"Edward"
{"stu01":"Alice","stu02":"Bob"}
"Alice"
Error!
对于本题目,查了一下,竟然都是对每个链状字符串(把从根key到目标key的所有字符串看做一条链),由于“ : ”最多为10000个,那么意味着最终要保存的hash值也就只有10000条,用hash的话冲突的概率极低,所以大家就妥妥的过了,只不过坑了乖孩子,不敢这么玩的人可就惨了。
example:
输入 :
{"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"}}
4
"hm"
"stu"
"stu"."stu01"
"students"
输出为:
"Edward"
{"stu01":"Alice","stu02":"Bob"}
"Alice"
Error!
对于本题目,查了一下,竟然都是对每个链状字符串(把从根key到目标key的所有字符串看做一条链),由于“ : ”最多为10000个,那么意味着最终要保存的hash值也就只有10000条,用hash的话冲突的概率极低,所以大家就妥妥的过了,只不过坑了乖孩子,不敢这么玩的人可就惨了。
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include <vector> #include <queue> #include <map> #include <string> using namespace std; #define ls (rt<<1) #define rs (ls|1) #define fst first #define snd second #define lson l,mid,ls #define rson mid+1,r,rs #define rep(i,n) for(int i=0;i<(int)n;i++) #define rep1(i,x,y) for(int i=x;i<=(int)y;i++) #define clr(a, x) memset(a, x, sizeof a) typedef long long LL; typedef long long ll; typedef pair<int,int> pii; const int N = 1010101; const int BASE = 131; inline int id(char c){return ((int)c)%(BASE); } int p; char str ,src ; map<LL,pii> M; void build(LL pre){ while(str[p]!='}'){ int now = pre; if(str[++p]=='}') return ; while(str[p]!=':'){ now=now*BASE+id(str[p]); p++; } int st = ++p; if(str[p]=='{'){ build(now*BASE+id('.')); } else { while(str[p+1]!=','&&str[p+1]!='}') p++; } M[now]=pii(st,p++); } } int main() { int T; scanf("%d",&T); while(T--){ M.clear(); scanf("%s",str); p = 0; build(0); int Q; scanf("%d",&Q); while(Q--){ scanf("%s",src); int len = strlen(src),res=0; rep(i,len) res=res*BASE+id(src[i]); if(!M.count(res)) printf("Error!\n"); else { pii te = M[res]; rep1(i,te.first,te.second) printf("%c",str[i]); printf("\n"); } } } return 0; }
相关文章推荐
- 服务器集群和负载均衡介绍
- Android官方命令深入分析之虚拟机
- 二分 例题3
- 打印二叉树中一个节点的所有祖先节点
- 黑马程序员——自学总结(九)正则表达式
- UML 类图基本图示法
- 读取properties文件
- 无法原谅自己的退步
- 二分 例题2
- 读取properties文件
- Android studio 重置 DDMS面板布局
- 二分 例题1
- 设计模式-前言
- HDU 4985 Little Pony and Permutation(简单模拟)
- Handler的用法大汇总
- 软件工程(8-10章)
- poj 3264 Balanced Lineup
- Can you find it?
- Ueditor上传图片到本地改造到上传图片到云存储
- C语言——原码、反码、补码、数据类型取值范围