Common Subexpression Elimination UVA - 12219

本文介绍了一种使用图论解决复杂数据结构问题的方法。通过自定义节点类和Map进行节点信息存储及查询去重,实现了高效的数据处理流程。文章详细展示了如何构建和解析表达式树,并提供了一个具体的代码实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图论的一道简单的题目,在进行实现的时候既要记录每个节点的信息,包括该节点所指向的右子树的节点同时也包括左子树的节点位置,以及当前节点当中所包含的字符的信息,同时利用一个Map来进行查询以及去重,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;

string express;

class Node{
public:
	int hash, left, right;
	string s;
	bool operator< (const Node a) const{
		if (hash != a.hash) return hash < a.hash;
		else if (left != a.left) return left < a.left;
		return right < a.right;
	}
};

Node node[50005];
map<Node, int> dict;

class Solve{
public:
	int amount;
	int finish[50005];
	int cur;
	
	int parse(int& ind){
		int id;
		id = amount++;
		Node& temp=node[id];
		temp.left = temp.right = -1;
		temp.hash = 0;
		temp.s = "";
		while (ind < express.size() && isalpha(express[ind])){
			temp.hash = temp.hash * 27 + express[ind] - 'a' + 1;
			temp.s += express[ind];
			ind++;
		}
		if (express[ind] == '('){
			ind++;
			temp.left = parse(ind);
			ind++;
			temp.right = parse(ind);
			ind++;
		}
		if (dict.count(temp) != 0){
			id--;
			amount--;
			return dict[temp];
		}
		return dict[temp] = id;
	}

	void Print(int ind){
		if (finish[ind] == cur){
			cout << ind + 1;
		}
		else{
			finish[ind] = cur;
			cout << node[ind].s;
			if (node[ind].left != -1){
				cout << "(";
				Print(node[ind].left);
				cout << ",";
				Print(node[ind].right);
				cout << ")";
			}
		}
	}

	void Deal(){
		cur++;
		amount = 0;
		int ind = 0;
		int res=parse(ind);
		Print(res);
		cout << endl;
	}
};

int  main(){
	int c;
	cin >> c;
	Solve a;
	a.cur = 0;
	memset(a.finish,0,sizeof(a.finish));
	while (c--){
		dict.clear();
		cin >> express;	
		a.Deal();
	}
}

`-O2` 是 GCC(GNU Compiler Collection)中的一个优化选项,用于在编译时对代码进行较高级别的优化。它在性能与编译时间之间取得了良好的平衡,是生产环境中常用的优化级别之一。 ### `-O2` 优化做了什么? GCC 的 `-O2` 优化选项会启用以下类型的优化(但不仅限于): 1. **指令调度(Instruction Scheduling)**:重新排列指令以更好地利用 CPU 流水线。 2. **循环展开(Loop Unrolling)**:减少循环的控制开销。 3. **常量传播(Constant Propagation)**:将变量替换为已知常量。 4. **死代码消除(Dead Code Elimination)**:移除不会被执行的代码。 5. **函数内联(Inlining)**:将小函数的代码直接插入调用处,减少函数调用开销。 6. **寄存器分配优化(Register Allocation)**:尽可能将变量放在寄存器中,而不是栈上。 7. **公共子表达式消除(Common Subexpression Elimination)**:避免重复计算相同的表达式。 ### 示例代码与编译对比 #### 示例 C 代码: ```c #include <stdio.h> int compute(int a, int b) { int result = 0; for (int i = 0; i < 1000; i++) { result += a * b + i; } return result; } int main() { printf("%d\n", compute(5, 10)); return 0; } ``` #### 编译命令: - **不优化**: ```bash gcc -O0 -S code.c -o code_O0.s ``` - **使用 `-O2` 优化**: ```bash gcc -O2 -S code.c -o code_O2.s ``` 你可以通过对比生成的汇编代码(`code_O0.s` 和 `code_O2.s`)看到优化带来的变化,例如循环被展开、不必要的操作被删除等。 ### 使用 `-O2` 的建议 - **适用场景**:适用于大多数性能敏感的项目,尤其是发布版本。 - **调试问题**:由于优化会改变代码结构,可能导致调试困难,建议调试时使用 `-O0`。 - **编译时间**:比 `-O1` 更耗时,但比 `-O3` 更快。 --- ### 相关问题 1. `-O1`, `-O2`, `-O3` 优化级别之间的区别是什么? 2. 如何查看 GCC 编译时启用了哪些优化选项? 3. `-O2` 优化会影响调试信息的准确性吗? 4. 在 CMake 中如何设置 `-O2` 作为默认优化级别?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值