AcWing 1225. 正则问题
原题链接
中等
作者:
wjie
,
2020-07-11 10:39:20
,
所有人可见
,
阅读 846
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
string str;
stack<int> nums;
stack<char> ops;
void getNumbers(int &i)
{
int number = 0;
while (str[i] == 'x')
{
number++;
i++;
}
nums.push(number);
}
void calc()
{
char op = ops.top();
ops.pop();
int a = nums.top();
nums.pop();
int b = nums.top();
nums.pop();
if (op == '|')
{
// printf("%d|%d\n", a, b);
nums.push(max(a, b));
}
else
{
// printf("%d&%d\n", a, b);
nums.push(a+b);
}
}
int main()
{
cin >> str;
str = '(' + str + ')';
for (int i = 0; i < str.size(); ++i)
{
if (str[i] == '(')
{
if (i-1 > 0 && (str[i-1] == 'x' || str[i-1] == ')'))
{
while (ops.top() == '&')
calc();
ops.push('&');
}
ops.push('(');
}
else if (str[i] == 'x')
{
if (str[i-1] == ')')
{
while (ops.top() == '&')
calc();
ops.push('&');
}
getNumbers(i);
i--;
}
else if (str[i] == '|')
{
while (ops.top() != '(')
calc();
ops.push('|');
}
else
{
while (ops.top() != '(')
calc();
ops.pop();
}
}
printf("%d", nums.top());
return 0;
}