UVA 12219 Common Subexpression Elimination(STL)
2015-04-01 13:47
295 查看
题意就是求最小的表达式树,也就是把相同的表达式子树给替换成最前面相同的编号。
做法:因为需要一一比较,所以把树给编号,方法是用根节点的字符串加左子树编号以及右子树编号。这样经过一次dfs就可以知道哪些是有重复的了。他要输出的编号可以用一个全局变量x,过程中遇到新的字符串就加加,如果这个树与子树是前面出现过的,那么就把x--。结构体里不仅要保存根节点字符串,左右子树编号,还要保存第一次出现的位置,这样才能满足输出的条件。
AC代码:
做法:因为需要一一比较,所以把树给编号,方法是用根节点的字符串加左子树编号以及右子树编号。这样经过一次dfs就可以知道哪些是有重复的了。他要输出的编号可以用一个全局变量x,过程中遇到新的字符串就加加,如果这个树与子树是前面出现过的,那么就把x--。结构体里不仅要保存根节点字符串,左右子树编号,还要保存第一次出现的位置,这样才能满足输出的条件。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000") #include<cstdio> #include<ctype.h> #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdlib> #include<stack> #include<queue> #include<set> #include<map> #include<cmath> #include<ctime> #include<string.h> #include<string> #include<sstream> #include<bitset> using namespace std; #define ll __int64 #define ull unsigned long long #define eps 1e-8 #define NMAX 1000000000 #define MOD 1000000 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define PI acos(-1) #define ALL(x) x.begin(), x.end() #define INS(x) inserter(x, x.end()) template<class T> inline void scan_d(T &ret) { char c; int flag = 0; ret=0; while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c == '-') { flag = 1; c = getchar(); } while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar(); if(flag) ret = -ret; } template<class T> inline T Max(T a, T b){ return a > b ? a : b;} template<class T> inline T Min(T a, T b){ return a < b ? a : b;} const int maxn = 50000+10; char ch[maxn*10]; int lch[maxn],rch[maxn],nct,pos; char op[maxn][6]; struct node { char s[6]; int l,r,wei; bool operator < (const node &t) const { int p = strcmp(s,t.s); if(p == 0) return l == t.l ? r < t.r : l < t.l; return p > 0 ? 0 : 1; } }; map<node, int>mp; vector<node>v; int flag[maxn],ji; bool pp; int ID(node x) { if(mp.find(x) != mp.end()) { pp = 1; return mp[x]; } v.push_back(x); return mp[x] = v.size()-1; } node make_node(char *s, int l, int r, int wei) { node t; strcpy(t.s,s); t.l = l; t.r = r; t.wei = wei; return t; } int build(int u) { int i = 0; while(ch[pos] >= 'a' && ch[pos] <= 'z') { op[u][i++] = ch[pos++]; } int qi = ++ji; op[u][i] = '\0'; // cout<<op[u]<<" "<<pos<<endl; if(ch[pos] == '(') { pos++; lch[u] = ++nct; int ID1 = build(lch[u]); pos++; rch[u] = ++nct; int ID2 = build(rch[u]); pos++; pp = 0; int ret = ID(make_node(op[u],ID1,ID2,qi)); if(pp) { flag[u] = v[ret].wei; ji--; } return ret; } pp = 0; int ret = ID(make_node(op[u],-1,-1,qi)); if(pp) { flag[u] = v[ret].wei; ji--; } // if(pos == 21) cout<<op[u]<<" "<<flag[u]<<" "<<qi<<endl; return ret; } void dfs(int u) { if(flag[u]) { printf("%d",flag[u]); return; } printf("%s",op[u]); if(lch[u] != 0) { printf("("); dfs(lch[u]); printf(","); dfs(rch[u]); printf(")"); } } int main() { #ifdef GLQ freopen("input.txt","r",stdin); // freopen("o.txt","w",stdout); #endif int cas; scanf("%d\n",&cas); while(cas--) { scanf("%s",ch); nct = 1; pos = ji = 0; mp.clear(); v.clear(); memset(flag,0,sizeof(flag)); memset(lch,0,sizeof(lch)); memset(rch,0,sizeof(rch)); build(1); dfs(1); printf("\n"); } return 0; }
相关文章推荐
- UVa 12219 Common Subexpression Elimination (stl,模拟,实现)
- 判断相同的子树--uva12219 Common Subexpression Elimination
- [hash 表达式树]UVa12219 - Common Subexpression Elimination
- hash 表达式树 UVa 12219 Common Subexpression Elimination
- UVA 12219 Common Subexpression Elimination(表达式树)
- UVA 12219 Common Subexpression Elimination (dfs瞎搞)
- UVA-12219 Common Subexpression Elimination
- 【紫书】(UVa12219)Common Subexpression Elimination
- Common Subexpression Elimination UVA - 12219
- UVA 12219 Common Subexpression Elimination——map
- uva 12219 Common Subexpression Elimination
- 12219 - Common Subexpression Elimination(表达式树)
- UVa #12219 Common Subexpression Elimination (例题11-1)
- uva12219 Common Subexpression Elimination
- [UVA] 11995 - I Can Guess the Data Structure! [STL应用]
- UVa 1593 (水题 STL) Alignment of Code
- UVA_10405 Longest Common Subsequence(DP)
- UVA 10405 - Longest Common Subsequence
- UVa - 136 - Ugly Numbers(STL - 优先队列priority_queue)
- UVA 10815 Andy's First Dictionary(stl,set)