#40209: c++解題方法


dvbdarcyvolleyball@gmail.com (kuhaku1027)


#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的用法練習