fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define GG ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  4. int main () { GG;
  5. int t; cin>>t;
  6. while(t--) {
  7. int n; string s;
  8. cin>>n>>s;
  9. stack<char> st;
  10.  
  11. for(int i=0;i<n;i++)
  12. { if(st.empty())
  13. st.push(s[i]);
  14. else if(s[i]== st.top())
  15. st.push(s[i]);
  16. else
  17. st.pop();
  18. }
  19. cout<<st.size()<<"\n";
  20. }return 0;}
Success #stdin #stdout 0.01s 5296KB
stdin
5
1
+
5
-----
6
+-+-+-
10
--+++++++-
20
+---++++-+++++---++-
stdout
1
5
0
4
4