fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fastIO() ios::sync_with_stdio(false); cin.tie(nullptr);
  5.  
  6. void solve() {
  7. int n;
  8. cin >> n;
  9.  
  10. string a, b;
  11. cin >> a >> b;
  12.  
  13. int pos = 0;
  14.  
  15. // Find best switching position
  16. while (pos + 1 < n && a[pos + 1] <= b[pos]) {
  17. pos++;
  18. }
  19.  
  20. // Build smallest string
  21. string result = a.substr(0, pos + 1) + b.substr(pos);
  22.  
  23. cout << result << "\n";
  24. cout << pos << "\n";
  25. }
  26.  
  27. int main() {
  28. fastIO();
  29. int t;
  30. cin >> t;
  31. while (t--) solve();
  32. return 0;
  33. }
Success #stdin #stdout 0s 5332KB
stdin
3
2
00
00
4
1101
1100
8
00100111
11101101
stdout
000
1
11000
2
001001101
6