(蓝桥)2017C/C++A组第七题正则问题

#include<iostream>  
#include<memory.h>
#include<stack>
#include<string>
using namespace std;
/*
主要思想是,利用栈,将输入的字符一个个压入栈中,遇到右括号,则弹出,直到
遇到左括号,然后将弹出的字符串进行解析,找到所代表的字符串,然后再压入栈中
最后栈中剩下的就是没有括号的表达式了,然后再解析一遍表达式,就得到答案了 
*/
int main()
{
    stack<char>sta;
    string re;//输入的表达式 
    cin >> re;
    for (int j = 0; j<re.length(); j++)
    {
        if (re[j] == '(' || re[j] == 'x' || re[j] == '|')
            sta.push(re[j]);
        if (re[j] == ')')
        {
            string out = "";//用来接收括号内的表达式 
            while (!sta.empty())
            {
                if (sta.top() == '(')//遇到左括号停止
                {
                    sta.pop();
                    break;
                }
                else
                {
                    char temp =sta.top();
                    out.push_back(temp);
                    sta.pop();
                }

            }
            //以下开始解析得到的字符串,用来求整个表达式的值 
            int max = 0;
            int count = 0;
            for (int i = 0; i<out.length(); i++)
            {
                if (out[i] == 'x')
                    count++;
                if (out[i] == '|')
                {
                    if (count>max)
                        max = count;
                    count = 0;
                }
            }
            if (count>max)
                max = count;
            for (int i = 0; i<max; i++)
            {
                sta.push('x');
            }
        }
    }
    
    //最后解析整个堆栈内存的字符串 
    int max = 0;
    int count = 0;
    while (!sta.empty())
    {
        if (sta.top() == 'x')
        {
            count++;
            sta.pop();
        }
        else if (sta.top() == '|')
        {
            sta.pop();
            if (count>max)
                max = count;
            count = 0;
        }
    }
    if (count>max)
        max = count;
    cout << max;
}

转载于:https://www.cnblogs.com/WAoyu/p/8516953.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值