zoj 3826 Hierarchical Notation(模拟)
2014-10-14 23:47
369 查看
题意:就是根据给定的语法查找某个标签之类的东西。。。
思路:嘛,看起来建个树然后查就ok了,现场的时候开始写的有些乱,建了一堆结构体什么的,后来发现只需要用个map搞一搞就好了。map的一个节点代表一个value值,然后map里一个映射<string,int>代表key对应的value是什么,然后就根据结构建树就好了,现场如果不算我sb把数组开小了可以算1A了,但是回来写wa了好久,貌似有些现场没有的奇怪数据,比如value是{}之类的?感觉有些蛋疼
代码:
思路:嘛,看起来建个树然后查就ok了,现场的时候开始写的有些乱,建了一堆结构体什么的,后来发现只需要用个map搞一搞就好了。map的一个节点代表一个value值,然后map里一个映射<string,int>代表key对应的value是什么,然后就根据结构建树就好了,现场如果不算我sb把数组开小了可以算1A了,但是回来写wa了好久,貌似有些现场没有的奇怪数据,比如value是{}之类的?感觉有些蛋疼
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<stack> #include<set> #include<cmath> #include<vector> #include<bitset> #define inf 0x3f3f3f3f #define Inf 0x3FFFFFFFFFFFFFFFLL #define eps 1e-6 #define pi acos(-1.0) using namespace std; typedef long long ll; const int maxn = 500000 + 10; char str[maxn],s[maxn]; map<string,int>mp[maxn]; string Ends[maxn],S[maxn]; int tot,negcnt; int solve(int L,int R) { bool flag = true; for(int i = L;i <= R;++i) if(str[i] == ',' || str[i] == ':') { flag = false; break; } if(flag) { negcnt++; Ends[negcnt] = ""; for(int i = L;i <= R;++i) Ends[negcnt] += str[i]; return -negcnt; } int id = ++tot; if(str[L] == '{' && str[R] == '}') {L++;R--;} int last = L,cnt = 0; for(int i = L;i <= R;++i) { if(str[i] == '{') cnt++; if(str[i] == '}') cnt--; if(str[i] == ',' && cnt == 0) { string tmp = ""; int j = last; while(str[j] != ':') tmp += str[j++]; mp[id][tmp] = solve(j + 1,i - 1); last = i + 1; } } string tmp = ""; int j = last; while(str[j] != ':') tmp += str[j++]; mp[id][tmp] = solve(j + 1,R); return id; } void print(int rt) { if(rt < 0) { cout<<Ends[-rt]; return ; } cout<<"{"; map<string,int>:: iterator it = mp[rt].begin(); cout<<(it -> first)<<":"; print(it -> second); it++; while(it != mp[rt].end()) { cout<<","<<(it -> first)<<":"; print(it -> second); it++; } cout<<"}"; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int t; scanf("%d",&t); while(t--) { scanf("%s",str); tot = negcnt = 0; int n = strlen(str); for(int i = 0;i <= n;++i) mp[i].clear(); solve(0,n - 1); // print(1); int q; scanf("%d",&q); while(q--) { scanf("%s",s); n = strlen(s); int m = 0; string tmp = ""; for(int i = 0;i < n;++i) if(s[i] == '.') { S[m++] = tmp; tmp = ""; } else tmp += s[i]; S[m++] = tmp; int now = 1; for(int i = 0;i < m;++i) { if(now < 0) {now = inf;break;} if(mp[now].find(S[i]) == mp[now].end()) { now = inf; break; } else now = mp[now][S[i]]; } if(now == inf) cout<<"Error!"<<endl; else { print(now); puts(""); } } } return 0; }
相关文章推荐
- zoj 3826 Hierarchical Notation(模拟)
- ZOJ 3826 Hierarchical Notation 模拟
- ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模拟)
- ZOJ 3826 Hierarchical Notation(模拟-字符串处理)
- ZOJ 3826 Hierarchical Notation(Mudanjiang 2014)(JSON解析模拟)
- ZOJ 3826 Hierarchical Notation 模拟
- zoj 3826 Hierarchical Notation(模拟)
- zoj 3826 Hierarchical Notation(模拟)
- ZOJ 3826 Hierarchical Notation(亚洲区域赛牡丹江站 字符串模拟)
- ZOJ 3826 Hierarchical Notation 模拟
- zoj 3826 Hierarchical Notation(hash+dfs)
- ZOJ 3826 Hierarchical Notation
- ZOJ-3826-Hierarchical Notation【哈希】
- ZOJ 3826 Hierarchical Notation(2014 牡丹江 B,树的直径+二分)
- ZOJ 3826 Hierarchical Notation(2014 牡丹江 D,概率DP)
- ZOJ 3826 Hierarchical Notation
- ZOJ 3826 Hierarchical Notation
- zoj3826 Hierarchical Notation
- ZOJ 3826 Hierarchical Notation(2014 Asia Mudanjiang Regional Contest)
- ZOJ 3829 Known Notation / 2014牡丹江区域赛 K (模拟, 贪心)