fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void solve() {
  6. string s;
  7. cin >> s;
  8. string t = "";
  9. int cnt = 0;
  10.  
  11. // Step 1: Move suffixes from `s` to `t` until `s` has only `0`s
  12. while (s.find('1') != string::npos) {
  13. size_t pos1 = s.find('1'); // Find the first occurrence of '1'
  14. t += s.substr(pos1); // Move that suffix to `t`
  15. s = s.substr(0, pos1); // Remove that suffix from `s`
  16. cnt++;
  17. }
  18.  
  19. // Step 2: Move suffixes from `t` back to `s` until `t` has only `1`s
  20. while (t.find('0') != string::npos) {
  21. size_t pos0 = t.find('0'); // Find the first occurrence of '0'
  22. s += t.substr(pos0); // Move that suffix back to `s`
  23. t = t.substr(0, pos0); // Remove that suffix from `t`
  24. cnt++;
  25. }
  26.  
  27. cout << cnt << endl;
  28. }
  29.  
  30. int main() {
  31. int t;
  32. cin >> t;
  33. while (t--) {
  34. solve();
  35. }
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 5284KB
stdin
5
5
00110
4
1111
3
001
5
00000
3
101
stdout
0
2
0
1
0