【題解】ZeroJudge a565: 2.p&q的邂逅

題目敘述:https://zerojudge.tw/ShowProblem?problemid=a565
注意:記得加上加速兩行,不然會TLE

  • ios_base::sync_with_stdio(0);
  • cin.tie(0);
#include <iostream>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T, p, ans;
    string s;

    cin >> T;
    while (T--) {
        cin >> s;
        p = 0;
        ans = 0;
        for (int i=0; i<s.size(); i++){
            if (s[i] == 'p'){
                p++;
            } else {
                if (s[i] == 'q' && p > 0){
                    p--;
                    ans++;
                }
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
分享本文 Share with friends