zoukankan      html  css  js  c++  java
  • 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;
    }

     


  • 相关阅读:
    请用正则实现String.trim()
    事件委托的原理是什么?有什么作用?
    请简述get请求和post请求的区别
    用原生js实现,点击一个列表时,输出对应的索引
    请用js写一个函数,实现获取浏览器url中查询字符串中的参数并返回一个数组
    请描述一下cookies、sessionStorage、localStorage、session四者的区别?
    清除浮动的几种方式,各自的优缺点?
    如何使用离线存储(localStorage)?
    使用css怎么让谷歌支持小于12px的文字比如10px
    ajax有哪些方法可以实现跨域?他们都有哪些局限性?
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350975.html
Copyright © 2011-2022 走看看