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. // Count number of valid paths
  24. long long countPaths = 1;
  25. int i = pos - 1;
  26.  
  27. while (i >= 0 && a[i + 1] == b[i]) {
  28. countPaths++;
  29. i--;
  30. }
  31.  
  32. cout << result << "\n";
  33. cout << countPaths << "\n";
  34. }
  35.  
  36. int main() {
  37. fastIO();
  38. int t;
  39. cin >> t;
  40. while (t--) solve();
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5316KB
stdin
3
2
00
00
4
1101
1100
8
00100111
11101101
stdout
000
2
11000
1
001001101
4