fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long solve(const string& s1, const string& s2) {
  5. int n = s1.size();
  6.  
  7. // count total potholes
  8. long long total = 0;
  9. for (int i = 0; i < n; i++) {
  10. total += (s1[i] == 'x') + (s2[i] == 'x');
  11. }
  12.  
  13. auto w = [&](int row, int col) {
  14. return (row == 0 ? s1[col] : s2[col]) == 'x' ? 1 : 0;
  15. };
  16.  
  17. // dp[row] = min potholes missed to reach this row at current col
  18. long long dp0 = w(0, 0);
  19. long long dp1 = w(1, 0);
  20.  
  21. for (int i = 0; i < n; i++) {
  22. // try switching lanes at current column
  23. long long new0 = min(dp0, dp1 + w(0, i));
  24. long long new1 = min(dp1, dp0 + w(1, i));
  25. dp0 = new0;
  26. dp1 = new1;
  27.  
  28. if (i == n - 1) break;
  29.  
  30. // move to next column
  31. dp0 += w(0, i + 1);
  32. dp1 += w(1, i + 1);
  33. }
  34.  
  35. return total - min(dp0, dp1);
  36. }
  37.  
  38. int main() {
  39. ios::sync_with_stdio(false);
  40. cin.tie(nullptr);
  41.  
  42. string s1, s2;
  43. cin >> s1 >> s2;
  44.  
  45. cout << solve(s1, s2) << "\n";
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
0