fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve()
  5. {
  6. int n;
  7. cin >> n;
  8.  
  9. string s;
  10. cin >> s;
  11.  
  12. int cnt = 0;
  13.  
  14. for(int i = 0; i < n - 1; i++)
  15. {
  16. if(s[i] != s[i + 1])
  17. cnt ++;
  18. }
  19.  
  20. if(cnt > 0 && s[0] == '0')
  21. cnt --;
  22.  
  23. cout << cnt << endl;
  24.  
  25. }
  26. int main() {
  27. // your code goes here
  28. int t;
  29. cin >> t;
  30.  
  31. while(t--)
  32. solve();
  33. }
Success #stdin #stdout 0s 5320KB
stdin
8
1
1
2
10
3
101
4
1100
5
11001
6
100010
10
0000110000
7
0101010
stdout
0
1
2
1
2
3
1
5