fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8.  
  9. int t;
  10. if (!(cin >> t)) return 0;
  11. while (t--) {
  12. int n;
  13. string s;
  14. cin >> n >> s;
  15. int firstOne = -1;
  16. int lastZero = -1;
  17. for (int i = 0; i < n; ++i) {
  18. if (s[i] == '1' && firstOne == -1) firstOne = i;
  19. if (s[i] == '0') lastZero = i;
  20. }
  21. if (firstOne == -1 || lastZero == -1 || firstOne > lastZero) {
  22. // no '1' before '0' => nothing to change
  23. cout << s << '\n';
  24. } else {
  25. // print leading zeros
  26. for (int i = 0; i < firstOne; ++i) cout << '0';
  27. // the single '0' in the middle (ONLY one char)
  28. cout << '0';
  29. // print trailing ones
  30. for (int i = lastZero + 1; i < n; ++i) cout << '1';
  31. cout << '\n';
  32. }
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty