UVa 12219 Common Subexpression Elimination (stl,模拟,实现)
一般来说,把一颗子树离散成一个int,把一个结点的字符离散成一个int会方便处理
直接map离散。当然一个结点最多只有4个小写字母,也可以直接编码成一个27进制的整数,舍掉0,为了区分0和0000。
需要注意的是有可能只有一个结点。
#include<bits/stdc++.h>
using namespace std; const int maxlen = 5e5;
const int maxn = 5e4+;
char str[maxlen]; map<string,int> smp;
map<string,int>::iterator sit;
vector<string> scache;
#define PB push_back
struct Node
{
int l,r;
int sid;
int Hash;
bool operator < (const Node& x) const{
return sid<x.sid || (sid==x.sid && (l < x.l || (l == x.l && r < x.r) ));
}
}tr[maxn]; map<Node,int> mp;
map<Node,int>::iterator it;
#define se second
#define fi first
#define MP make_pair int id_cnt; int SID(string &s)
{
if((sit = smp.find(s)) != smp.end()) return sit->se;
scache.PB(s);
smp.insert(MP(s,scache.size()));
return scache.size();
} int ID(Node &nd)
{
if((it = mp.find(nd)) != mp.end()) return it->se;
mp.insert(MP(nd,id_cnt));
tr[id_cnt] = nd;
return id_cnt++;
} int build(int x,int &len)
{
Node u; u.Hash = ;
string s;
int i;
for(i = x; str[i]; i++){
if(str[i] == ',' || str[i] == ')'){
s.assign(str+x,str+i);
u.sid = SID(s);
len = s.size();
u.l = u.r = -;
return ID(u);
}else if(str[i] == '('){
s.assign(str+x,str+i);
u.sid = SID(s);
int lsz, rsz;
u.l = build(++i,lsz);
i += lsz;
u.r = build(++i,rsz);
len = s.size()+lsz+rsz+;
return ID(u);
}
}
s.assign(str+x,str+i);//只有一个结点
u.sid = SID(s);
u.l = u.r = -;
return ID(u);
} int dfs_clock; void dfs(int u)
{
if(!tr[u].Hash){
tr[u].Hash = ++dfs_clock;
printf("%s",scache[tr[u].sid-].c_str());
}else {
printf("%d",tr[u].Hash); return;
}
if(~tr[u].l){
putchar('(');
dfs(tr[u].l);
putchar(',');
dfs(tr[u].r);
putchar(')');
}
} int main()
{
//freopen("in.txt","r",stdin);
int T; scanf("%d\n",&T);
while(T--){
gets(str);
mp.clear(); id_cnt = ;
scache.clear(); smp.clear();
int len;
int root = build(,len);
dfs_clock = ;
dfs(root);putchar('\n');
}
return ;
}
UVa 12219 Common Subexpression Elimination (stl,模拟,实现)的更多相关文章
- UVA 12219 Common Subexpression Elimination
题意: 求最小的表达式树,也就是把相同的表达式子树给替换成最前面相同的编号. 分析: 用map<string,int>smp;存放子树对应的字符串,如果以后出现相同的子树则用相同编号表示. ...
- 【uva 12219】Common Subexpression Elimination(图论--树+自定义比较器+映射+递归)
题意:如题,用表达式树来表示一个表达式,且消除公共的部分,即用编号表示.编号 K 定义为表达式第 K 个出现的字符串. 解法:先构造表达式树,给每棵子树用(string,left_son,right_ ...
- 「日常训练」Common Subexpression Elimination(UVa-12219)
今天做的题目就是抱佛脚2333 懂的都懂. 这条题目干了好几天,最后还是参考别人的代码敲出来了,但是自己独立思考了两天多,还是有收获的. 思路分析 做这条题我是先按照之前的那条题目(The SetSt ...
- stl+模拟 CCF2016 4 路径解析
// stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...
- 【STL+模拟】UVa 506 - System Dependencies
System Dependencies Components of computer systems often have dependencies--other components that m ...
- UVA - 11995 - I Can Guess the Data Structure! STL 模拟
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
- uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟
由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...
- STL——模拟实现空间配置器
目录 问题 SGI版本空间配置器-std::alloc 一级空间配置器 二级空间配置器 Refill.chunkAlloc函数 最后,配置器封装的simple_alloc接口 问题 我们在日常编写C+ ...
- uva 210 - Concurrency Simulator (并行程序模拟)
from CSDN: https://blog.csdn.net/su_cicada/article/details/87898579 例题6-1 并行程序模拟( Concurrency Simula ...
随机推荐
- HDOJ-2037
今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Flutter实战视频-移动电商-25.列表页_使用Provide控制子类-1
25.列表页_使用Provide控制子类-1 主要是二级分类的UI布局 1分15秒 生成我们的右侧动态类 定义list变量 开始写里面的子项,把每一个小的写了 再拼成一个大的 这样我们的小类就写完了 ...
- YUV格式学习:YUV420P、YV12、NV12、NV21格式转换成RGB24(转载)
转自:http://www.latelee.org/my-study/yuv-learning-yuv420p-to-rgb24.html 对于YUV420的格式,网上有一大堆资料,这里就不说了.直奔 ...
- hoj2798 Globulous Gumdrops
Globulous Gumdrops My Tags (Edit) Source : 2008 Stanford Programming Contest Time limit : 1 se ...
- 多线程 GCD队列组
// DYFViewController.m // 623-08-队列组 // // Created by dyf on 14-6-23. // Copyright (c) 2014年 ___ ...
- Linux权限相关
权限分组 用户:文件所有者 用户组:多个用户的集合 其他:除了用户和用户组之外的任何用户 权限类别 r:表示读的权限 w:表示写的权限 x:表示执行的权限 s:表示setuid权限,允许用户以其拥有者 ...
- ios开发static关键字的理解
:static关键字修饰局部变量::当static关键字修饰局部变量时,该局部变量只会初始化一次,在系统中只有一份内存 :static关键字不可以改变局部变量的作用域,但是可延长局部变量的生命周期,该 ...
- Testing Complex Logic with JMeter Beanshell
BeanShell是最先进的JMeter内置组件之一.JMeter具有丰富的内置插件,可满足性能测试的许多需求.例如,在编写一些复杂的测试时,您可能需要一些额外的脚本.在这种情况下,值得使用Beans ...
- __getitem__,__setitem__,__delitem__
__getitem__.__setitem__.__delitem__ 总结: __getitem__,__setitem_,__delitem__ : obj[‘属性’]的方式去操作属性时触发的方法 ...
- [51Nod1952] 栈
Description 不支持后端删除的dequeue,每次操作后查询最大值. \(n\leq10^7\).时限1.5s,不用考虑读入/输出复杂度. Solution 首先考虑如果没有后端删除怎么做, ...