求最长有效括号子串的长度
作者:
XDgbh | 来源:发表于
2018-08-22 22:28 被阅读20次#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int max = 0;
stack<int> st;
int length = str.length();
for (int i = 0; i < length; i++)
{
if (')'== str[i] && '(' == str[st.top()] && !st.empty())
{
st.pop();
if (st.empty())
{
max = i + 1;
}
else if (i-st.top()>max)
{
max = i - st.top();
}
}
else
{
st.push(i); //左括号入栈
}
}
cout << max << endl;
return 0;
}
本文标题:求最长有效括号子串的长度
本文链接:https://www.haomeiwen.com/subject/pnsuiftx.html
网友评论