poj 1690 (Your)((Term)((Project)))

来源:互联网 发布:开启式电机铭牌数据 编辑:程序博客网 时间:2024/04/25 21:26
(Your)((Term)((Project)))
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2826 Accepted: 1063

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 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-BA-(B+C)
题意:在给出的式子中省略空格和多余的括号,然后输出结果
思路:括号要删除有3种情况:
(1)“(”在第一个位置;
(2)括号里没有运算符,即没有“+”或“-”;
(3)括号里面有运算符,但是“(”前面不是“-”。
做法:从里到外删除括号,先扫描一次,找出每个与“)”匹配的“(”的位置。然后再扫描一次,遇到“)”就操作,标记要删除的位置。
AC代码:
#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <queue>#include <cmath>#include <vector>#include <cstdlib>#include <iostream>using namespace std;int main(){    char s[300],str[300];    int t;    int match[300];    bool dele[300];    cin>>t;    getchar();    while(t--)    {        memset(match,-1,sizeof(match));        gets(str);        int i=0,j=0;        for(i=0,j=0; str[i]; i++)            if(str[i]!=' ') s[j++]=str[i];        s[j]='\0';        //printf("%s\n",s);        for(i=0; s[i]; i++)        {            if(s[i]==')')                for(int j=i-1; j>=0; j--)                    if(s[j]=='('&&match[j]==-1)                    {                        match[i]=j;                        match[j]=i;                        break;                    }        }        memset(dele,false,sizeof(dele));        for(i=0; s[i]; i++)        {            if(s[i]==')'&&!dele[i])            {                bool flag=false;                for(j=i-1;j>match[i];j--)                if(s[j]=='+'||s[j]=='-')                {                    flag=true;                    break;                }                //'('在第一个位置||括号里没有运算符||括号里面有运算符,但是'('前面不是减号                if(match[i]==0||!flag||(s[match[i]-1]!='-'&&flag))                {                    dele[i]=true;                    dele[match[i]]=true;                }            }        }        for(int i=0;s[i];i++)        if(!dele[i]) printf("%c",s[i]);        printf("\n");    }    return 0;}


原创粉丝点击