Assignment - 6 Daa
Assignment - 6 Daa
Vishw Vekariya
U21CS104
1.) CODE:
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
//bottom-up DP
//iterative approach
int LPSI(string a, string b)
{
vector<int> cur(b.length()+ 1, 0);
vector<int> next(b.length()+ 1, 0);
//top-down DP
int LPSTD(string &a, string &b, int i, int j, vector<vector<int>>&dp)
{
//base case
if(i == a.length())
{
return 0;
}
if(j == b.length())
{
return 0;
}
if (dp[i][j] != -1)
{
return dp[i][j];
}
if (a[i] == b[j])
{
ans = 1+ LPSTD(a, b, i+1, j+1, dp);
}
else
{
ans = max(LPSTD(a, b, i, j+1, dp), LPSTD(a, b, i+1, j, dp));
}
return dp[i][j] = ans;
}
//recursive approach
int LPSR(string &a, string &b, int i, int j)
{
//base case
if(i == a.length())
{
return 0;
}
if(j == b.length())
{
return 0;
}
if (a[i] == b[j])
{
ans = 1+ LPSR(a, b, i+1, j+1);
}
else
{
ans = max(LPSR(a, b, i, j+1), LPSR(a, b, i+1, j));
}
return ans;
}
2.)
CODE:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
if (dp[n][m] != -1)
return dp[n][m];
else
{
int mini = min(edittopDown(str1, str2, n, m - 1, dp),
edittopDown(str1, str2, n - 1, m, dp));
mini = min(mini, edittopDown(str1, str2, n - 1, m - 1, dp));
return dp[n][m] = (1 + mini);
}
}
return dp[n][m];
}
return prev[m];
}
int main()
{
string a = "tis";
string b = "nisarg";
int n = a.length();
int m = b.length();
vector<vector<int> > dp(n + 1, vector<int>(m + 1, -1));
OUTPUT: