ZOJ Problem Set - 1423 (Your)((Term)((Project)))

一道比较好的题,对栈和队列有很大的提高!

给几个测试数据~~

(a-(b-c))
((((a)-(a+b)-((a+b))-(a+b)+a)))
A-((B+C))

答案自己看就知道了!第二个数据很容易错的!

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
stack<bool> s;
int main()
{
    //freopen("a.txt","r",stdin);
    char ch[300],src[300],tch[300],aim[300];
    int len,i,cnt1,cnt2,k,cas;
    bool flag;
    cin>>cas;
    getchar();
    while(cas --)
    {
        cin.getline(ch,300);
        len = strlen(ch);
        k = 0;
        cnt1 = 0;
        cnt2 = 0;
        for(i = 0;i < len;i ++)
        {
            if(ch[i] !=' ') {src[k] = ch[i]; k++;}
        }

        len = k;
        k = 0;
        for(i = 0;i < len;i ++)
        {
            if(i>0&&src[i] == '('&&src[i-1] == '-')
            {
                 tch[k++] = src[i];
                 flag = 1;
                 s.push(flag);
                 continue;
            }
            if((i>0&&src[i] == '('&&src[i-1] == '+')||(src[i] == '('&&src[i-1]=='(')||(src[i]=='('&&i==0) )
            {
                 flag = 0;
                 s.push(flag);
                 continue;
            }
            if(src[i] == ')')
            {
                if(!s.empty()) {
                flag = s.top();
                s.pop();
                if(flag == 1) {tch[k++] = ')';}
                }
                continue;
            }
            tch[k ++] = src[i];

        }
        len = k;
        k = 0;
        for(i = 0;i < len;i ++)
        {
            if(tch[i] == '('&&tch[i+2] ==')')
               continue;
            if(tch[i] == ')'&&tch[i-2]=='(')
               continue;
            aim[k++] = tch[i];
        }
        for(i = 0;i < k;i ++)
        cout<<aim[i];
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值