Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- using namespace std;
- string s1, s2;
- int dp[5005][5005];
- int rec(int i, int j) {
- if(i == -1) {
- return j + 1;
- }
- if(j == -1) {
- return i + 1;
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int result = 2e9;
- if(s1[i] == s2[j]) {
- result = min(result, rec(i - 1, j - 1));
- }
- result = min(result, rec(i - 1, j) + 1);
- result = min(result, rec(i, j - 1) + 1);
- result = min(result, rec(i - 1, j - 1) + 1);
- return dp[i][j] = result;
- }
- int main() {
- cin >> s1 >> s2;
- memset(dp, -1, sizeof dp);
- cout << rec(s1.size() - 1, s2.size() - 1) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement