ZOJ 1423 (Your)((Term)((Project)))


末尾多了个空格,wa到天亮。。。。。


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

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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)


Source: Asia 1999, Tehran (Iran)

去掉前面不是减号的括号,去掉前面是减号但括号范围内没有符号的括号,特别要注意     —((A+B))这样的


#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

char str[500];
int display[300];
char temp[500];
int lc;

int pll(int s)
{
    int pol[300];
    memset(pol,0,sizeof(pol));

    int lp=1;
    pol[s]=1;
    int i;
    for(i=s+1;i<=lc&&lp;i++)
    {
   //     cout<<"--> "<<lp<<endl;
        if(str=='(')
        {
            lp++;
            pol=lp;
        }
        else if(str==')')
        {
            pol=lp;
            lp--;
        }
        else
            pol=lp;
    }

    int ts=s-1;
    while(display[ts]==0)
    {
        ts--;
    }

    if(str[ts]!='-')
    {
        display[s]=display[i-1]=0;
        return -1;
    }
    int OK=0;

    for(int j=s;j<=i-1;j++)
    {
        if((str[j]=='+'||str[j]=='-')&&pol[j]==1)
        {
            OK=1;
            break;
        }
    }

    if(OK==0)
    {
        display[s]=display[i-1]=0;
        return -1;
    }
    return 0;
}

int main()
{
    int T;
    scanf("%d",&T);
    getchar();
while(T--)
{
    memset(str,0,sizeof(str));
    memset(display,-1,sizeof(display));
    char ss[500];
    gets(ss);

    int len=strlen(ss);
    lc=1;  str[0]='+';
    for(int i=0;i<len;i++)
    {
        if(ss!=' ')
        {
            str[lc]=ss;
            lc++;
        }
    }

    len=strlen(str);
    for(int i=1;i<=lc;i++)
    {
        if(str=='(')
        {
            pll(i);
        }
    }

        display[0]=0;

    for(int i=0;i<lc;i++)
    {
        if(display==-1)
            putchar(str);
    }
    putchar(10);
}
    return 0;
}

 


posted @ 2013-07-12 17:15  码代码的猿猿  阅读(309)  评论(0编辑  收藏  举报