规则 :给定一个只包含两种字符的字符串:( ,) ,其他字符均为无效
1、任何左括号 ( 必须有相应的右括号 )。
2、任何右括号 ) 必须有相应的左括号 ( 。
3、左括号 ( 必须在对应的右括号之前 )。
4、有效字符的长度为左括号和右括号的相加。
#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
int calc(const string& str,int& tail) {
stack<char> s;
int max = 0;
int count = 0;
while (tail >= 0) {
if (str[tail] == '(' && s.size() == 0 || (str[tail]!=')' && str[tail]!='(')) {
while (s.size() != 0) {
s.pop();
}
printf("str is %c\n", str[tail]);
}
else if (str[tail] == '(' && s.top() == ')') {
s.pop();
++count;
}
else if (str[tail] == ')') {
s.push(str[tail]);
}
--tail;
}
if (max < count) {
max = count;
}
printf("max is %d\n", max);
return max*2;
}
int main(int argc, char* argv[]) {
string str;
cin >> str;
int len = str.length();
if (len < 2)
cout << 0 << endl;
--len;
cout << calc(str,len) << endl;
getchar();
return 0;
}