Advertisement
Hezov

Three strings

Jun 30th, 2025 (edited)
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. // https://codeforces.com/contest/2050/problem/E
  2.  
  3. #include <iostream>
  4. using namespace std;
  5. typedef long long ll;
  6. const int INF = 1e9;
  7. int dp[1001][1001];
  8. void solve()
  9. {
  10.     string a, b, c;
  11.     cin >> a >> b >> c;
  12.     for(int i = 0;i<=a.length();i++)
  13.         for(int j = 0;j<=b.length();j++)
  14.         {
  15.             if(i == 0 && j == 0) continue;
  16.             dp[i][j] = INF;
  17.             int aPoz = i-1, bPoz = j-1, cPoz = i + j - 1;
  18.             if(j-1>=0)
  19.                 dp[i][j] = min(dp[i][j], dp[i][j-1] + (b[bPoz] != c[cPoz]));
  20.             if(i-1>=0)
  21.                 dp[i][j] = min(dp[i][j], dp[i-1][j] + (a[aPoz] != c[cPoz]));
  22.         }
  23.     int aLen = a.length(), bLen = b.length(), cLen = c.length();
  24.     cout << min(dp[aLen][cLen-aLen],dp[cLen-bLen][bLen]) << '\n';
  25.      for(int i = 0;i<=a.length();i++)
  26.         for(int j = 0;j<=b.length();j++)
  27.             dp[i][j] = 0;
  28.  
  29. }
  30. int main()
  31. {
  32.     ios_base::sync_with_stdio(false);
  33.     cin.tie(0); cout.tie(0);
  34.     int t; cin >> t;
  35.     while(t--) solve();
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement