专栏首页算法修养POJ--1690 (Your)((Term)((Project)))(字符串处理)

POJ--1690 (Your)((Term)((Project)))(字符串处理)

(Your)((Term)((Project))) Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3353 Accepted: 1256 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: The input file contains a number of expressions, each in one separate line. Variables in the expressions are only single uppercase letters. 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 <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>

using namespace std;
char a[300];
char b[300];
int tag[300];
int flag[300];
int len;
stack<int> s;
int find(int num)
{
    int res=0;
    for(int i=0;i<len;i++)
    {
        if(tag[i]==num)
            res=i;
    }
    return res;
}
int fun(int x,int y)
{
    for(int i=x;i<=y;i++)
        if(a[i]=='+'||a[i]=='-')
            return 0;
    return 1;
}
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        memset(a,0,sizeof(a));
        gets(b);
        int tot=0;
        int pot=0;
        while(b[tot]!='\0')
        {
            if(b[tot]!=' ')
            {
               a[pot]=b[tot];
                pot++;
            }
            tot++;
        }
        len=strlen(a);
        while(!s.empty())
            s.pop();
        int cot=0;
        memset(tag,0,sizeof(tag));
        for(int i=0;i<len;i++)
        {
            if(a[i]=='(')
            {
                tag[i]=++cot;
                s.push(i);
            }

            else if(a[i]==')')
            {
                tag[i]=tag[s.top()];
                s.pop();
            }
            else
                tag[i]=0;

        }
        memset(flag,0,sizeof(flag));

       for(int i=0;i<len;i++)
          {
            if(a[i]=='('&&i==0)
                  flag[tag[i]]=1;
            if(a[i]=='('&&a[i-1]=='+')
                  flag[tag[i]]=1;
           // if(a[i]=='('&&a[i+2]==')')
                  //flag[tag[i]]=1;
            if(a[i]=='('&&a[i-1]=='(')
                  flag[tag[i]]=1;
            if(a[i]=='('&&(fun(i+1,find(tag[i])-1)==1)&&a[i-1]=='-')
                  flag[tag[i]]=1;
          }
        for(int i=0;i<len;i++)
            if(flag[tag[i]]==0)
                printf("%c",a[i]);
        printf("\n");
    }
    return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • HDU 3367 Pseudoforest(Kruskal)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/655...

    ShenduCC
  • ZOJ 3326 An Awful Problem(模拟)

    An Awful Problem ---- Time Limit: 1 Second      Memory Limit: 32768 KB ---- In o...

    ShenduCC
  • ZOJ 3204 Connect them

    Connect them ---- Time Limit: 1 Second      Memory Limit: 32768 KB ---- You have...

    ShenduCC
  • HDU 3367 Pseudoforest(Kruskal)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/655...

    ShenduCC
  • BZOJ 3053: The Closest M Points(K-D Tree)

    attack
  • POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submission...

    Angel_Kitty
  • leetcode480. Sliding Window Median

    Median is the middle value in an ordered integer list. If the size of the list i...

    眯眯眼的猫头鹰
  • POJ--1936 All in All(水题,暴力即可)

    All in All Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3...

    ShenduCC
  • POJ 3292 Semi-prime H-numbers

    Description This problem is based on an exercise of David Hilbert, who pedagogi...

    attack
  • Leetcode solution 272: Closest Binary

    https://blog.baozitraining.org/2019/05/leetcode-solution-272-closest-binary.html

    包子面试培训

扫码关注云+社区

领取腾讯云代金券