You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
// 中缀转后缀
unordered_map<char, int> g{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}}; // 操作符优先级
string infixToPostfix(string s) {
string t; // 结果串
stack<char> stk; // 栈
for (int i = 0; i < s.size(); i++) {
// ①数字
if (isdigit(s[i])) {
int x = 0;
while (i < s.size() && isdigit(s[i])) {
x = x * 10 + s[i] - '0';
i++;
}
i--;
t += to_string(x), t += ' ';
} else if (isalpha(s[i])) // ②字符,比如a,b,c
t += s[i], t += ' ';
else if (s[i] == '(') // ③左括号
stk.push(s[i]); // 左括号入栈
else if (s[i] == ')') { // ④右括号
while (stk.top() != '(') { // 让栈中元素(也就是+-*/和左括号)一直出栈,直到匹配的左括号出栈
t += stk.top(), t += ' ';
stk.pop();
}
stk.pop(); // 左括号也需要出栈
} else {
// ⑤操作符 +-*/
while (stk.size() && g[s[i]] <= g[stk.top()]) {
t += stk.top(), t += ' ';
stk.pop();
}
stk.push(s[i]); // 将自己入栈
}
}
// 当栈不为空时,全部输出
while (stk.size()) {
t += stk.top(), t += ' ';
stk.pop();
}
return t;
}
// 后缀转表达式树
const int N = 500, M = N << 1;
#define ls e[h[u]] // 左儿子
#define rs e[ne[h[u]]] // 右儿子,由于表达式树是二叉树,有右儿子时一定有左儿子
// 链式前向星
int e[M], h[N], idx, ne[M];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
char a[N];
int al = 25; // a[]:节点号对应的字符,0~25为数字a~z在表达式树中节点的编号,26及以上为每一个操作符的编号注意如果前后出现多次同一运算符它们的节点号是不一样的
// 构造表达式树
int build(string postfix) {
stack<int> stk; // 节点号栈
// 链式前向星初始化
memset(h, -1, sizeof h);
idx = 0, al = 25;
// 后缀表达式->表达式树,可以转化:前缀表达式,中缀表达式,后缀表达式
for (int i = 0; i < postfix.size(); i++) { // 枚举后缀表达式每一个字符
char c = postfix[i];
if (c == ' ') continue;
if (isalpha(c))
stk.push(c - 'a'); // 不是操作符,是数字,按字符-'a'的数字号入栈
else { // 如果是运算符
int x = stk.top(); // 取出两个节点
stk.pop();
int y = stk.top();
stk.pop();
a[++al] = c; // 操作符,申请一个节点号,并且,记录下来节点号与原字符的关系
add(al, x), add(al, y); // 由操作符向左右两个儿子连出边
stk.push(al); // 操作符节点入栈
}
}
return stk.top(); // 栈顶部的字符就是根
}
// 输出节点u
char out(int u) {
if (u >= 26) return a[u]; // 节点号大于等于26的是操作符+-*/
return u + 'a'; // a~z,记录的是0~25,输出需要加上偏移量'a'
}
// 判断操作符
bool isOp(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// 获取符合题意的中缀表达式
string inorder(int u) {
string ans = "";
if (h[u] == -1) {
ans += out(u);
return ans;
}
string lstr = inorder(ls);
string rstr = inorder(rs);
// 下面的代码是本题的题意要求
if (isOp(a[ls]) && g[a[u]] > g[a[ls]]) { // 如果左儿子是操作符,并且当前运算符优先级大于左儿子运算优先级,需要左儿子加上括号
ans += '(';
ans += lstr;
ans += ')';
} else
ans += lstr; // 否则,左儿子不加括号
ans += out(u); // 左儿子完事,把自己加上
if (isOp(a[rs]) && g[a[rs]] <= g[a[u]]) { // 如果右儿子是操作符,并且,右儿子运算符优先级小于等于当前运算符优先级
if ((a[u] == '+' && a[rs] == '-') // 当前是+,右儿子是-
|| (a[u] == '*' && a[rs] == '/') // 当前是*,右儿子是/
|| (a[u] == '+' && a[rs] == '+') // 当前是+,右儿子是+
|| (a[u] == '*' && a[rs] == '*')) // 当前是*,右儿子是*
ans += rstr; // 都不需要加括号
else
ans += '(' + rstr + ')'; // 否则需要加括号
} else
ans += rstr; // 否则右儿子不加括号
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("HDU1123.in", "r", stdin);
freopen("HDU1123.out", "w", stdout);
#endif
int n;
cin >> n;
while (n--) {
string infix;
cin >> infix;
// 中缀转后缀
string postfix = infixToPostfix(infix);
// 根据后缀表达式创建表达式树
int root = build(postfix);
cout << inorder(root) << endl;
}
return 0;
}