zoukankan      html  css  js  c++  java
  • POJ 1690 (Your)((Term)((Project)))

    (Your)((Term)((Project)))
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 2912   Accepted: 1084

    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-B
    A-(B+C)
    题目大意:去除一个表达式中的括号,使其含义不变。
    解题方法:通过分析可知,可去除的括号有三类,1.最外层括号 2.前面为加号的括号 3.括号中只有一个字母的括号。
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char str[300];
        char str1[300];
        int pre[300];
        int visited[300];
        int del[300];
        int nCase;
        scanf("%d", &nCase);
        getchar();
        while(nCase--)
        {
            gets(str);
            memset(pre, 0, sizeof(pre));
            memset(visited, 0, sizeof(visited));
            memset(del, 0, sizeof(del));
            int nLen = strlen(str);
            int nCount = 0;
            for (int i = 0; i < nLen; i++)
            {
                if (str[i] != ' ')
                {
                    str1[nCount++] = str[i];
                }
            }
            str1[nCount] = '';
            nLen = strlen(str1);
            for (int i = 0; i < nLen; i++)
            {
                if (str1[i] == ')')
                {
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (str1[j] == '(' && !visited[j])
                        {
                            pre[i] = j;
                            visited[j] = 1;
                            break;
                        }
                    }
                }
            }
            for (int i = 0; i < nLen; i++)
            {
                if (str1[i] == ')')
                {
                    int flag = 0;
                    for (int j = i - 1; j > pre[i]; j--)
                    {
                        if (str1[j] == '+' || str1[j] == '-')
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if (str1[pre[i]] == '(' && (str1[pre[i] - 1] != '-' || pre[i] == 0 || !flag))
                    {
                        del[i] = del[pre[i]] = 1;
                    }
                }
            }
            for (int i = 0; i < nLen; i++)
            {
                if (!del[i])
                {
                    printf("%c", str1[i]); 
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    使用Windows Live Writer发布日志
    下雪
    Oracle中拼出树型结构
    [转载]Javascript中最常用的55个经典技巧
    博客访问者来自15个国家和地区
    [转载]一个帐号同一时间只能一个人登录
    换了博客的皮肤
    常见的开源软件许可
    java5中的Arrays
    青花瓷
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3212569.html
Copyright © 2011-2022 走看看