(Your)((Term)((Project)))
You have typed the report of your term project in your personal computer. There are several one line arithmetic expressions in your report. There is no redundant parentheses in the expressions (omitting a pair of redundant matching parentheses does not change the value of the expression). In your absence, your little brother inserts some redundant matching parentheses in the expressions of your report. Assume that the expressions remain syntactically correct and evaluate to their original value (the value before inserting redundant parentheses). To restore your report to its original form, you are to write a program to omit all redundant parentheses.
To make life easier, consider the following simplifying assumptions:
The input file contains a number of expressions, each in one separate line.
Variables in the expressions are only single uppercase letters.
Operators in the expressions are only binary ‘+’ and binary ‘-‘.
Note that the only transformation allowed is omission of redundant parentheses, and no algebraic simplification is allowed.
Input
The input file consists of several test cases. The first line of the file contains a single number M, which is the number of test cases (1 <= M <= 10). Each of the following M lines, is exactly one correct expression. There may be arbitrarily space characters in each line. The length of each line (including spaces) is at most 255 characters.
Output
The output for each test case is the same expression without redundant parentheses. Notice that the order of operands in an input expression and its corresponding output should be the same. Each output expression must be on a separate line. Space characters should be omitted in the output expressions.
Sample Input
3
(A-B + C) - (A+(B - C)) - (C-(D- E) )
((A)-( (B)))
A-(B+C)
Sample Output
A-B+C-(A+B-C)-(C-(D-E))
A-B
A-(B+C)
题目链接:ZOJ-1423
题目大意:给出一个表达式,只需要去除冗余括号,不需要任何代数化简!
题目思路:模拟
去除以下括号:
1.A+(A+B)
2.((A+B))
3.(B)
注意:
1.括号,需对应去除
2.开头空格注意
给出两个特殊样例
1. (a- ( ( a)-(a)))
2. (a-(v+v)-c)
以下是代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <stack>
using namespace std;
int match[1000];
int del[1000];
vector <char> ans;
int main()
{
int t;
cin >> t;
getchar();
while(t--)
{
string s;
getline(cin,s);
s = "+" + s;
int len = s.size();
int cnt = 0;
ans.clear();
memset(del, 0, sizeof del);
memset(match, 0, sizeof match);
//匹配括号
stack <char> st;
for (int i = 0; i < len; i++)
{
if (s[i] == '(') st.push(i);
else if (s[i] == ')')
{
int pos = st.top();
match[i] = pos;
match[pos] = i;
st.pop();
}
}
for (int i = 0; i < len; i++)
{
if (s[i] == ' ') continue;
if (del[i]) continue;
if (s[i] == '(')
{
if (ans[ans.size() - 1] == '+' || ans[ans.size() - 1] == '(')
{
del[i] = 1;
del[match[i]] = 1;//对应括号删除
}
else
{
ans.push_back(s[i]);
}
}
else if (s[i] == ')')
{
ans.push_back(s[i]);
//====处理(B)的情况
int len2 = ans.size();
if (len2 >= 2 && ans[len2 - 3] == '('){
char ch = ans[len2 - 2];
ans.pop_back();
ans.pop_back();
ans.pop_back();
ans.push_back(ch);
}
//=====
}
else ans.push_back(s[i]);
}
int len2 = ans.size();
for (int i = 1; i < len2; i++) cout << ans[i];
cout << endl;
}
return 0;
}