#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; }