Advertisement
ivnikkk

Untitled

May 15th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS  
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(pref) pref.begin(), pref.end()
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8. typedef long double ld;
  9. signed main() {
  10. #ifdef _DEBUG
  11.     freopen("input.txt", "r", stdin);
  12.     freopen("output.txt", "w", stdout);
  13. #endif
  14.     ios_base::sync_with_stdio(false);
  15.     cin.tie(nullptr);
  16.     cout.tie(nullptr);
  17.     const ll inf = 1e18;
  18.     ll t;
  19.     cin >> t;
  20.     while (t--) {
  21.         ll n;
  22.         cin >> n;
  23.         vector<string> a(2);
  24.         cin >> a[0];
  25.         cin >> a[1];
  26.         pll f, l;
  27.         for (ll i = 0; i < n; i++) {
  28.             bool fl = false;
  29.             for (ll j = 0; j < 2; j++) {
  30.                 if (a[j][i] == '*') {
  31.                     f = { j,i };
  32.                     fl = true;
  33.                     break;
  34.                 }
  35.             }
  36.             if (fl)break;
  37.         }
  38.         for (ll i = n-1; i>=0; i--) {
  39.             bool fl = false;
  40.             for (ll j = 0; j < 2; j++) {
  41.                 if (a[j][i] == '*') {
  42.                     l = { j,i };
  43.                     fl = true;
  44.                     break;
  45.                 }
  46.             }
  47.             if (fl)break;
  48.         }
  49.         vector<vector<ll>> dp(2, vector<ll>(n, 0));
  50.         for (ll i = f.second; i <= l.second; i++) {
  51.             if (i > 0 && i!=f.second) {
  52.                 dp[1][i] = dp[1][i - 1] + 1;
  53.                 dp[0][i] = dp[0][i - 1] + 1;
  54.                 dp[1][i] = min(dp[1][i], dp[0][i] + 1);
  55.                 dp[0][i] = min(dp[0][i], dp[1][i] + 1);
  56.             }
  57.             if (a[0][i] == '*') {
  58.                 if (a[1][i] == '*') {
  59.                     dp[0][i] = min(dp[1][i] + 1, dp[0][i] + 1);
  60.                     dp[1][i] = dp[0][i];
  61.                 }
  62.                 else {
  63.                     dp[1][i]++;
  64.                 }
  65.                
  66.             }
  67.             else if (a[1][i] == '*') {
  68.                 dp[0][i]++;
  69.             }
  70.         }
  71.         cout << dp[l.first][l.second] << '\n';
  72.  
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement