fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define f1(i, n) for(int i=1;i<=n;++i)
  4. #define all(x) x.begin(), x.end()
  5. using namespace std;
  6. const int MOD = 1e9 + 7;
  7. const int maxn = 1e6 + 5;
  8. int main() {
  9. ios::sync_with_stdio(false);
  10. cin.tie(nullptr);
  11.  
  12. int t;
  13. cin >> t;
  14. while (t--) {
  15. int n;
  16. string s;
  17. cin >> n >> s;
  18. int count_0 = 0, count_1 = 0, max_run_0 = 0, max_run_1 = 0;
  19. int curr_0 = 0, curr_1 = 0;
  20. for (int i = 0; i < s.size(); ++i) {
  21. if (s[i] == '0') {
  22. count_0++;
  23. curr_0++;
  24. max_run_0 = max(max_run_0, curr_0);
  25. curr_1 = 0;
  26. }
  27. else {
  28. count_1++;
  29. curr_1++;
  30. max_run_1 = max(max_run_1, curr_1);
  31. curr_0 = 0;
  32. }
  33. }
  34. cout << min(count_1 + 2 * (count_0 - max_run_0), count_0 + 2 * (count_1 - max_run_1)) << "\n";
  35. }
  36.  
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
0
0