您的位置:首页 > 其它

(Your)((Term)((Project)))

2015-11-04 19:23 411 查看
Description

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:

1. The input file contains a number of expressions, each in one separate line.

2. Variables in the expressions are only single uppercase letters.

3. 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)

这道题可以不需要用栈,直接处理就行,去掉括号的情况有三种。

1.一对括号的前面还是括号

2.一对括号的前面时+号

3.一对括号前面没有字符。

但是这道题我没注意的地方就是一个右括号匹配完左括号后,此左括号就不能再用了。

#include <stdio.h>
#include <string.h>
const int maxn = 305;
char str[maxn];
int vis[maxn], del[maxn];
//开始只用一个数组错了好多次,没有看清要匹配括号
int main ( )
{
int T, len, cnt;
scanf ( "%d", &T );
getchar ( );
while ( T -- )
{
memset ( vis, 0, sizeof ( vis ) );
memset ( del, 0, sizeof ( del ) );
gets ( str );
len = strlen ( str );
cnt = 0;
for ( int i = 0; i < len; i ++ )
if ( str[i] != ' ' )
str[cnt ++] = str[i];   //去掉空格
str[cnt] = '\0';
for ( int i = 0; i < cnt; i ++ )
{
if ( str[i] == ')' )
{
int flag = 0, j;
for ( j = i-1; j >= 0; j -- )
{
if ( vis[j] == 0 && str[j] == '(' ) //当到左括号时必须退出循环
{
if ( ! j || str[j-1] != '-' || flag == 0 )
del[i] = del[j] = 1;
//前面没有字符或前面的字符不为-或此括号对中没有符号
vis[j] = 1;
//**此左括号已和后面的右括号匹配了,所以需要标记
break ;
}
if ( str[j] == '+' || str[j] == '-' )
//查看此括号对中是否有符号
flag = 1;
}
}
}
for ( int i = 0; i < cnt; i ++ )
if ( ! del[i] )
printf ( "%c", str[i] );
printf ( "\n" );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: