知识点:栈
这个题一开始卡了我一会儿,看了题解下面的一句话,栈存下标不是字符才想起来怎么做的,然后就过了这个题,看了看和闫雪灿写的一模一样,这就是现在的我,就是个半吊子水平,有提示是可以写出来的,但是到了考场上面是没有人给你提示的
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
stack<int> st;
int ans = 0;
for (int i = 0; i < (int) s.size(); i++) {
if (!st.empty()) {
if (s[st.top()] == '(' && s[i] == ')') st.pop();
else if (s[st.top()] == '[' && s[i] == ']') st.pop();
else if (s[st.top()] == '{' && s[i] == '}') st.pop();
else st.push(i);
} else {
st.push(i);
}
ans = max(ans, i - (st.empty() ? -1 : st.top()));
}
cout << ans;
return 0;
}