UVA 12219 Common Subexpression Elimination——map
2017-10-07 11:26
288 查看
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <map> using namespace std; const int MAXN = 1e6; struct Node { char s[10]; int cnt, num, lch, rch; bool operator < (const Node &temp) const { if (num != temp.num) return num < temp.num; if (lch != temp.lch) return lch < temp.lch; return rch < temp.rch; } }; struct Tree { Node node[MAXN]; int tot, pos; char str[MAXN]; map<Node, int> m; bool vis[MAXN]; void init() { scanf("%s", str + 1); tot = 0, pos = 1; m.clear(); } int build() { int root = ++tot; node[root].cnt = node[root].num = node[root].lch = node[root].rch = 0; vis[root] = false; while ('a' <= str[pos] && str[pos] <= 'z') { node[root].s[++node[root].cnt] = str[pos]; node[root].num = node[root].num * 30 + str[pos] - 'a' + 1; ++pos; } if (str[pos] == '(') { ++pos; node[root].lch = build(); ++pos; node[root].rch = build(); ++pos; } int x = m[node[root]]; if (x) { --tot; return x; } else return m[node[root]] = root; } void output(int root) { if (vis[root]) printf("%d", root); else { vis[root] = true; for (int i = 1; i <= node[root].cnt; i++) printf("%c", node[root].s[i]); if (node[root].lch) { printf("("); output(node[root].lch); printf(","); output(node[root].rch); printf(")"); } } } }tree; int main() { int T; scanf("%d", &T); while (T--) { tree.init(); tree.build(); tree.output(1); printf("\n"); } return 0; }
相关文章推荐
- UVA 12219 Common Subexpression Elimination(STL)
- 【紫书】(UVa12219)Common Subexpression Elimination
- Common Subexpression Elimination UVA - 12219
- UVa 12219 Common Subexpression Elimination (stl,模拟,实现)
- uva 12219 Common Subexpression Elimination
- UVA 12219 Common Subexpression Elimination(表达式树)
- [hash 表达式树]UVa12219 - Common Subexpression Elimination
- UVA 12219 Common Subexpression Elimination (dfs瞎搞)
- UVA-12219 Common Subexpression Elimination
- hash 表达式树 UVa 12219 Common Subexpression Elimination
- 判断相同的子树--uva12219 Common Subexpression Elimination
- 12219 - Common Subexpression Elimination(表达式树)
- uva12219 Common Subexpression Elimination
- UVa #12219 Common Subexpression Elimination (例题11-1)
- UVa Problem 10252 Common Permutation (公共排列)
- UVa 13011 Height map Latin America Regional Contest 2015
- UVA 10405 Longest Common Subsequence(最长公共子序列)
- UVa 10405 - Longest Common Subsequence
- UVa 11991 Easy Problem from Rujia Liu? / STL-MAP
- MS-SQL2005新特性(Common Table Expression)