zoukankan      html  css  js  c++  java
  • (Your)((Term)((Project)))(字符串处理)

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

    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)
    

    Source

    [Submit]   [Go Back]   [Status]   [Discuss]



     1 /*
     2 Source Code
     3 Problem: 1690        
     4 Memory: 184K        Time: 0MS
     5 Language: C++        Result: Accepted
     6 */
     7 
     8     #include <iostream>
     9     #include <cstdio>
    10 
    11     using namespace std;
    12 
    13     char s[300], d[300];
    14 
    15     void func(int i)  //去掉')'
    16     {
    17         int cnt = 0;
    18         for(i++; s[i] != '\0'; i++)
    19         {
    20             if(s[i] == '(') cnt++;
    21             else if(s[i] == ')')
    22             {
    23                 if(!cnt)
    24                 {
    25                     s[i] = ' ';
    26                     return ;
    27                 }
    28                 cnt--;
    29             }
    30         }
    31     }
    32 
    33     int main()
    34     {
    35         int n, i, j;
    36         scanf("%d", &n);
    37         getchar();
    38         while(n--)
    39         {
    40             gets(s);
    41             for(i = 0, j = -1; s[i] != '\0'; i++)
    42             {
    43                 if(s[i] == ' ') continue;
    44                 else if(s[i] == '(')
    45                 {
    46                     if(j == -1 || d[j] == '+' || d[j] == '(') func(i);
    47                     else if(d[j] == '-')
    48                     {
    49                         int k, cnt = 0, tag = 0;
    50                         for(k = i + 1; s[k] != '\0'; k++)
    51                         {
    52                             if(s[k] == '(') cnt++;
    53                             else if(s[k] == ')')
    54                             {
    55                                 if(!cnt) break;
    56                                 cnt--;
    57                             }
    58                             else if(s[k] == '+' || s[k] == '-')
    59                             {
    60                                 if(!cnt)
    61                                 {
    62                                     tag = 1;
    63                                     break;
    64                                 }
    65                             }
    66                         }
    67                         if(tag) d[++j] = s[i];
    68                         else func(i);
    69                     }
    70                 }
    71                 else d[++j] = s[i];
    72             }
    73             d[++j] = '\0';
    74             printf("%s\n", d);
    75         }
    76         return 0;
    77     }




  • 相关阅读:
    Excel技巧大全
    2019年6月27日单词
    HTML5(12) 实时通讯WebSocket
    C#(99):随机数Random
    C#(99):C# 8.0 的新特性( NET Framework 4.8 与 Visual Studio 2019 )
    2019年6月13日单词
    2019年6月5日单词
    2019年5月30日单词
    used to do 与be used to doing /n.
    Json.Net(一)介绍
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910548.html
Copyright © 2011-2022 走看看