模拟栈操作,遇见a就count++,遇见b就count--(ab配对)
在循环中如果count<0说明b比a先出现,就返回Bad
#include <iostream> using namespace std; int main() { string s; cin >> s; int count = 0; for (int i = 0; i < s.size(); i++) { if(count<0){ cout<<"Bad"; return 0; } if (s[i] == 'a')count++; else { count--; } } if (count == 0)cout << "Good"; else cout << "Bad"; }