c++编程:堆栈小疑惑,求大佬们解答
两种情况的区别:
1、
stack位置在两者之间,提交时显示:段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起case通过率为60.00%
2、
stack位置在两者之后,提交时显示:答案正确:恭喜!您提交的程序通过了所有的测试用例
源码
#笔试题目##include<iostream> #include<string> #include<stack> using namespace std; int main() { string s; cin >> s; stack<char>a; for (int i = 0; i < s.size(); i++){ if (s[i] == '[' || s[i] == '('){ a.push(s[i]); } else if (s[i] == ']'){ if (a.top() == '[') a.pop(); else{ cout << "false"; return 0; } } else if (s[i] == ')'){ if (a.top() == '(') a.pop(); else{ cout << "false"; return 0; } } else continue; } if (a.empty()) cout << "true"; else cout << "false"; return 0; }