#40209: c++解題方法


dvbdarcyvolleyball@gmail.com (kuhaku1027)

學校 : 新北市私立南山高級中學
編號 : 266888
來源 : [118.166.199.123]
最後登入時間 :
2025-06-15 17:11:54

#include <iostream>
#include <stack>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);//加速

  int n;
  cin >> n;
  string a;
  for(int i = 0;i < n;i++){
    int ans = 0;
    stack <char> stk;
      cin >> a;    
    for(int j = 0;j < a.length();j++){
        if(a[j] == '.'){
          continue;
        }         
        else if(a[j] == 'p'){
          stk.push('p');
        }         
        else if(a[j] == 'q'){
          if(!stk.empty()){
            ans += 1;
            stk.pop();
          }
        }
    }
    cout << ans << "\n";   
  } 
}

stack的用法練習