表达式树方法求调
查看原帖
表达式树方法求调
928972
ny_Dacong楼主2024/9/14 16:49

应该是乘方的问题,第二小问计算过程有问题。

比如:

2^(2+2)*2^(2+2+2)*2^2^2-1

同时有一个点 TLE,用时很不正常,应该是死递归了。

代码有点长,求高人指点。当局者迷旁观者清,我现在晕了。看不懂的变量名可以楼下问。

#include<bits/stdc++.h>
using namespace std;
int n,R,Leaf;
char str[120];
int root[120],ls[120],rs[120],deep[120],num[120];
bool isnum(int pos){
	return str[pos] >= '0' && str[pos] <= '9';
}
int getroot(int pos){
	if(root[pos] == 0){
		return pos;
	}
	return getroot(root[pos]);
}
void maketree(int Start){
	int Last = 0,l,r,Floor = 0;
	for(int i = Start; i <= n; i++){
		Last = i;
		if(str[i] == '('){
			Floor++;
			maketree(i+1);
		}
		if(str[i] == ')'){
			Floor--;
			if(Floor < 0){
				break;
			}
		}
	}
	Last--;
	for(int i = Last; i >= Start; i--){
		if(str[i] == '('){
			Floor++;
		}
		if(str[i] == ')'){
			Floor--;
		}
		if(Floor){
			continue;
		}
		if(str[i] == '^'){
			l = i,r = i;
			while(!isnum(l)){
				l--;
			}
			while(!isnum(r)){
				r++;
			}
			l = getroot(l),r = getroot(r);
			root[l] = root[r] = i;
			ls[i] = l,rs[i] = r;
		}
	}
	Floor = 0;
	for(int i = Start; i <= Last; i++){
		if(str[i] == '('){
			Floor++;
		}
		if(str[i] == ')'){
			Floor--;
		}
		if(Floor){
			continue;
		}
		if(str[i] == '*' || str[i] == '/'){
			l = i,r = i;
			while(!isnum(l)){
				l--;
			}
			while(!isnum(r)){
				r++;
			}
			l = getroot(l),r = getroot(r);
			root[l] = root[r] = i;
			ls[i] = l,rs[i] = r;
		}
	}
	Floor = 0;
	for(int i = Start; i <= Last; i++){
		if(str[i] == '('){
			Floor++;
		}
		if(str[i] == ')'){
			Floor--;
		}
		if(Floor){
			continue;
		}
		if(str[i] == '+' || str[i] == '-'){
			l = i,r = i;
			while(!isnum(l)){
				l--;
			}
			while(!isnum(r)){
				r++;
			}
			l = getroot(l),r = getroot(r);
			root[l] = root[r] = i;
			ls[i] = l,rs[i] = r;
		}
	}
	return;
}
void getdeep(int now){
	deep[now] = deep[root[now]]+1;
	if(ls[now] && rs[now]){
		getdeep(ls[now]);
		getdeep(rs[now]);
	}
}
void PRINT(int now){
	if(ls[now] && rs[now]){
		PRINT(ls[now]);
		PRINT(rs[now]);
	}else{
		if(deep[now] > deep[Leaf]){
			Leaf = now;
		}
	}
	if(num[now] == 0x7f7f7f7f){
		printf("%c ",str[now]);
	}else{
		printf("%d ",num[now]);
	}
}
int power(int di,int zhi){
	int ans = 1;
	while(zhi){
		if(zhi&1){
			ans *= di;
		}
		di *= di;
		zhi >>= 1;
	}
	return ans;
}
int main(){
//	freopen("huan.in","r",stdin);
//	freopen("huan.out","w",stdout);
	scanf("%s",str+1);
	n = strlen(str+1);
	maketree(1);
	for(int i = 1; i <= n; i++){
		if(isnum(i)){
			R = getroot(i);
			break;
		}
	}
	deep[R] = 1;
	getdeep(R);
	memset(num,0x7f,n*4+20);
	while(Leaf != R){
		Leaf = 0;
		PRINT(R);
		static int tp;
		if(num[ls[root[Leaf]]] == 0x7f7f7f7f){
			num[ls[root[Leaf]]] = str[ls[root[Leaf]]]-'0';
		}
		if(num[rs[root[Leaf]]] == 0x7f7f7f7f){
			num[rs[root[Leaf]]] = str[rs[root[Leaf]]]-'0';
		}
		if(str[root[Leaf]] == '^'){
			tp = power(num[ls[root[Leaf]]],num[rs[root[Leaf]]]);
		}
		if(str[root[Leaf]] == '*'){
			tp = num[ls[root[Leaf]]]*num[rs[root[Leaf]]];
		}
		if(str[root[Leaf]] == '/'){
			tp = num[ls[root[Leaf]]]/num[rs[root[Leaf]]];
		}
		if(str[root[Leaf]] == '+'){
			tp = num[ls[root[Leaf]]]+num[rs[root[Leaf]]];
		}
		if(str[root[Leaf]] == '-'){
			tp = num[ls[root[Leaf]]]-num[rs[root[Leaf]]];
		}
		for(int i = ls[root[Leaf]]; i <= rs[root[Leaf]]; i++){
			num[i] = tp;
		}
		ls[root[Leaf]] = 0,rs[root[Leaf]] = 0;
		printf("\n");
	}
	return 0;
}

2024/9/14 16:49
加载中...