ZOJ 3826 Hierarchical Notation
2016-05-24 20:54
399 查看
思路:哈希之后一直爬就是了
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=10010;
int T,q,n,bp
,ep
,ch
,bro
;
char s[N*20*2],w
[25],ss[N*20*2];
int dfs(int x,int &pos)
{
//puts(s+x);
int i=x,tt=n,t;
while(s[i]!='}')
{
int j=0;
while(s[i]!=':') w
[j++]=s[i++];
w
[j]=0;
//puts(w
);
bro
=ch
=0;
i++;// :
t=n++;
bp[t]=i;
if(s[i]=='{') { ch[t]=dfs(i+1,ep[t]);i=++ep[t]; }
else
{
while(s[i]!='}'&&s[i]!=',') i++;
ep[t]=i;
}
bro[t]=n;
if(s[i]==',') i++;
//puts(s+i);
}
bro[t]=0;
pos=i;
if(tt==n) return 0;
return tt;
}
/*
const int HASH=10007;
char e
[25];
struct Hash
{
int *head,*bro,n;
Hash()
{
head=new int[HASH];
bro=new int
;
n=1;
}
int hash(char*s)
{
int ans=1;
for(int i=0;s[i];i++) ans*=s[i];
if(ans<0) ans=-ans;
return ans%HASH;
}
bool cmp(char*a,char*b) { return !strcmp(e[a],e[b]); }
bool insert(int s)
{
int h=hash(e[s]);
int u=head[h];
return 1;
}
};
*/
char st[25];
int find(char *ss,int u)
{
u=ch[u];//走到ch
int i=0;
//puts(ss);
while(ss[i]!='.'&&ss[i]!=0) { st[i]=ss[i];i++; }
st[i]=0;
//puts(st);
//printf("%d\n",i);
for(;u;u=bro[u]) if(!strcmp(w[u],st)) break;
if(!u) return 0;
if(!ss[i]) return u;
return find(ss+i+1,u);
}
void read(int u)
{
u=ch[u];
for(;u;u=bro[u]) printf("%s:%d %d %d %d\n",w[u],bro[u],ch[u],bp[u],ep[u]);
for(;u;u=bro[u]) if(ch[u]) read(u);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%s%d",s,&q);
n=1;
ch[0]=dfs(1,ep[0]);
/* for(int u=1;u<n;u++)
{
char c=s[ep[u]];
s[ep[u]]=0;
puts(s+bp[u]);
s[ep[u]]=c;
printf("%s:%d %d %d %d\n",w[u],bro[u],ch[u],bp[u],ep[u]);
}
read(0);
*/ while(q--)
{
scanf("%s",ss);
int x=find(ss,0);
if(!x) puts("Error!");
if(!x) continue;
char c=s[ep[x]];
s[ep[x]]=0;
puts(s+bp[x]);
s[ep[x]]=c;
}
}
return 0;
}
Description
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 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
Sample Output
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=10010;
int T,q,n,bp
,ep
,ch
,bro
;
char s[N*20*2],w
[25],ss[N*20*2];
int dfs(int x,int &pos)
{
//puts(s+x);
int i=x,tt=n,t;
while(s[i]!='}')
{
int j=0;
while(s[i]!=':') w
[j++]=s[i++];
w
[j]=0;
//puts(w
);
bro
=ch
=0;
i++;// :
t=n++;
bp[t]=i;
if(s[i]=='{') { ch[t]=dfs(i+1,ep[t]);i=++ep[t]; }
else
{
while(s[i]!='}'&&s[i]!=',') i++;
ep[t]=i;
}
bro[t]=n;
if(s[i]==',') i++;
//puts(s+i);
}
bro[t]=0;
pos=i;
if(tt==n) return 0;
return tt;
}
/*
const int HASH=10007;
char e
[25];
struct Hash
{
int *head,*bro,n;
Hash()
{
head=new int[HASH];
bro=new int
;
n=1;
}
int hash(char*s)
{
int ans=1;
for(int i=0;s[i];i++) ans*=s[i];
if(ans<0) ans=-ans;
return ans%HASH;
}
bool cmp(char*a,char*b) { return !strcmp(e[a],e[b]); }
bool insert(int s)
{
int h=hash(e[s]);
int u=head[h];
return 1;
}
};
*/
char st[25];
int find(char *ss,int u)
{
u=ch[u];//走到ch
int i=0;
//puts(ss);
while(ss[i]!='.'&&ss[i]!=0) { st[i]=ss[i];i++; }
st[i]=0;
//puts(st);
//printf("%d\n",i);
for(;u;u=bro[u]) if(!strcmp(w[u],st)) break;
if(!u) return 0;
if(!ss[i]) return u;
return find(ss+i+1,u);
}
void read(int u)
{
u=ch[u];
for(;u;u=bro[u]) printf("%s:%d %d %d %d\n",w[u],bro[u],ch[u],bp[u],ep[u]);
for(;u;u=bro[u]) if(ch[u]) read(u);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%s%d",s,&q);
n=1;
ch[0]=dfs(1,ep[0]);
/* for(int u=1;u<n;u++)
{
char c=s[ep[u]];
s[ep[u]]=0;
puts(s+bp[u]);
s[ep[u]]=c;
printf("%s:%d %d %d %d\n",w[u],bro[u],ch[u],bp[u],ep[u]);
}
read(0);
*/ while(q--)
{
scanf("%s",ss);
int x=find(ss,0);
if(!x) puts("Error!");
if(!x) continue;
char c=s[ep[x]];
s[ep[x]]=0;
puts(s+bp[x]);
s[ep[x]]=c;
}
}
return 0;
}
Description
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 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!
相关文章推荐
- VIM教程 Learn Vim Progressively
- 数据结构----快速排序
- Java之3.Math类、Date、SimpleDateFormat
- 关于SLAM的那些事——实时RGBD_ORB_SLAM (Ubuntu+Xtion)
- poj 2109
- 赫夫曼树
- javascript之DOM编程增加附件
- [Codeforces226D]The table(构造+贪心)
- Android四大组件(1.2)--Activity的启动模式
- ZOJ 3822 Domination(概率DP)
- Android各层日志打印功能
- javascript之DOM编程增加附件
- Spring单例模式与线程安全
- 第二次冲刺周期06
- Android实现双击事件的两种方式
- putty显示乱码解决方法
- HDU 5699 (线性规划 二分)
- Cookie详解
- Poj 1856 Sea Battle【Dfs+思维】
- ZOJ 3819 Average Score