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 ...
随机推荐
- SSE2 Intrinsics各函数介绍
http://blog.csdn.net/fengbingchun/article/details/18460199
- 甩掉 ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练
在WebForm下 开发ajax程序,需要借助于一般处理程序(*.ashx)或web服务(*.asmx),并且每一个ajax请求,都要建一个这样的文件,如此一来,如果在一个项目中ajax程序多了,势必 ...
- 3DMAX 4角色蒙皮
1 角色建模 略,以后补充 2 骨骼绑定 一般不用骨骼直接拉,Biped足够,以后适当补充骨骼直接拉的操作 1 将Biped骨骼和模型对齐 1 创建biped之后,第一步一要先选择,然后再对位骨骼到模 ...
- luoguP4242树上的毒瘤
传送门 模板集合吧,除了码农,没啥难的... 和bzoj2243:[SDOI2011]染色十分相像,但是多了点集和查询的区别 然后点集显然可以看出是虚树问题,查询可以用点分治\(O(nlogn)\), ...
- jstl标签库jar包下载
- java数据结构----队列,优先级队列
1.队列:和栈中的情况不同,队列中的数据项不总是从数组下标0开始,移除一个数据项后,队头指针会指向下标较高的数据项,其特点:先入先出 2.图解 3.队列的实现代码: 3.1.Queue.java pa ...
- 当前View的坐标相对其他View的位置坐标
// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect - (CGRect)convertRect:(CGRect)rect toView:(UIView *) ...
- tcp端口扫描(python多线程)
1 使用单线程扫描单台主机 首先实现的是对单台主机中0-1024端口的扫描,发现差不多每秒扫描一个端口,很慢. import socket def tcp_scanner(host,port): cl ...
- collections 中 typing 中对象的引用
from typing import ( Callable as Callable, Container as Container, Hashable as Hashable, Iterable as ...
- HDU-1029-Ignatius aned the Princess IV
链接:https://vjudge.net/problem/HDU-1029#author=0 题意: 给你n个数字,请你找出出现至少(n+1)/2次的数字. 思路: dp,hash超时了,不知道是不 ...