Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <queue>
- using namespace std;
- const int INF = 1e9;
- const int maxn = 5005;
- string A, B;
- int dp[maxn][maxn];
- int rec(int i, int j) {
- if(i < 0) {
- return j + 1;
- }
- if(j < 0) {
- return i + 1;
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int res = INF;
- if(A[i] == B[j]) {
- res = min(res, rec(i - 1, j - 1));
- }
- res = min(res, rec(i - 1, j) + 1);
- res = min(res, rec(i, j - 1) + 1);
- res = min(res, rec(i - 1, j - 1) + 1);
- dp[i][j] = res;
- return res;
- }
- int main() {
- for(int i = 0; i < maxn; i++) {
- for(int j = 0; j < maxn; j++) {
- dp[i][j] = -1;
- }
- }
- cin >> A >> B;
- cout << rec(A.size() - 1, B.size() - 1) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment