AcWing 1225. 正则问题(栈版)
原题链接
中等
作者:
刷题使我着迷版
,
2025-04-10 20:20:20
· 广东
,
所有人可见
,
阅读 1
#include <iostream>
#include <vector>
#include <cstring>
#include <stack>
#include <cmath>
using namespace std;
string s;
stack<char> st;
void check()
{
int count=0;int max_n =0;
while(!st.empty() && st.top() != '(')
{
int cur =st.top();st.pop();
if(cur == 'x') count++;
else
{
max_n = max(count,max_n);
count=0;
}
}
max_n = max(count,max_n);
st.pop();
for(int i=1;i<=max_n;i++)
{
st.push('x');
}
}
int main()
{
cin>>s;
int n = s.size();
st.push('(');
for(int i=0;i<n;i++)
{
if(s[i] != ')') st.push(s[i]);
else check();
}
check();
int res = st.size();
cout<<res<<endl; return 0;
}