UVALive - 4882 Parenthesis 表达式处理、字符串处理、栈

本文介绍了一种用于去除数学表达式中多余括号的算法。该算法通过扫描输入字符串并根据运算符的特性来判断括号是否可以省略,旨在提高人类阅读自动产生的表达式的便利性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4882 - Parenthesis
Time limit: 3.000 seconds
To a computer, there is no difference between the expression (((x)+(y))(t)) and(x+y)t; but, to a human, the latter is easier to read. When writing automatically generated expressions that a human may have to read, it is useful to minimize the number of parentheses in an expression. We assume expressions consist of only two operations: addition (+) and multiplication (juxtaposition), and these operations act on single lower-case letter variables only. Specifically, here is the grammar for an expressionE:

E : P | P ‘+’ E
P : F | F P
F : V | ‘(’ E ‘)’
V : ‘a’ | ‘b’ | .. | ‘z’
The addition (+, as in x+y) and multiplication (juxtaposition, as inxy) operators are associative: x+(y+z)=(x+y)+z=x+y+z and x(yz)=(xy)z=xyz. Commutativity and distributivity of these operations should not be assumed. Parentheses have the highest precedence, followed by multiplication and then addition.

Input
The input consists of a number of cases. Each case is given by one line that satisfies the grammar above. Each expression is at most 1000 characters long.

Output
For each case, print on one line the same expression with all unnecessary parentheses removed.

Sample input
x
(x+(y+z))
(x+(yz))
(x+y(x+t))
x+y+xt
Sample Output
x
x+y+z
x+yz
x+y(x+t)
x+y+xt

Source
UVALive - 4882
Output
题意:给一个有括号小写字母加号组成的字符串,去掉多余的括号后输出。
code by:xuhuang

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,s,f,n;
    char a[1010];
    while(~scanf("%s",a))
    {
        n=strlen(a);
        for(i=0; a[i]; i++)
        {
            if(a[i]=='(')
            {
                s=0;
                f=0;
                for(j=i+1; a[j]; j++)
                {
                    if(a[j]==')')
                    {
                        if(s==0)break;//说明找到和a[i]匹配的右括号
                        else s--;
                    }
                    if(s==0&&a[j]=='+')f=1;//说明该括号内是多项式
                    if(a[j]=='(')
                    {
                        s++;
                    }
                }
                if(f==1)
                {
                    if(i>0&&a[i-1]!='+'&&a[i-1]!='(');//并且括号外有多项式和其相乘
                    else if(j<n-1&&a[j+1]!='+'&&a[j+1]!=')');
                    else
                    {
                        for(; a[j]; j++)
                        {
                            a[j]=a[j+1];
                        }
                        for(j=i; a[j]; j++)
                        {
                            a[j]=a[j+1];
                        }
                        n-=2;
                        i--;
                    }
                }
                else
                {
                    for(; a[j]; j++)
                    {
                        a[j]=a[j+1];
                    }//右括号右边的向左移
                    for(j=i; a[j]; j++)
                    {
                        a[j]=a[j+1];
                    }//左括号右边的向左移
                    n-=2;
                    i--;
                }
            }
        }
        printf("%s\n",a);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值