题目见描述
思路:考察了对栈的理解运用:只要遇到a就入栈,遇到b就让a出栈,如果此时出现栈空则返回false,最后检查一下如果栈不为空也返回false
代码:
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
string str;
stack<char> stk;
int main()
{
cin >> str;
bool flag = true;
for (int i = 0; i < str.size(); i ++ )
{
if (str[i] == 'a') stk.push('a');
else
{
if (stk.empty())
{
flag = false;
break;
}
stk.pop();
}
}
if (!stk.empty()) flag = false;
if (flag) cout << "Good" << endl;
else cout << "Bad" << endl;
return 0;
}
京公网安备 11010502036488号