用栈来模拟。栈中的a遇到串里的b时就匹配上。
#include <bits/stdc++.h> using namespace std; char trans(char a) { if (a == 'b') return 'a'; return '@'; } int main() { stack <char> sta; string s; cin >> s; while (!sta.empty()) sta.pop(); for (int i = 0; i < s.size(); i++) { if (sta.empty()) { sta.push(s[i]); continue; } if (trans(s[i]) == sta.top()) { sta.pop(); } else sta.push(s[i]); } if (sta.empty()) cout << "Good"; else cout << "Bad"; return 0; }