forked from rajatjain-21/Algorithmic-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDP(Basic).cpp
More file actions
34 lines (31 loc) · 710 Bytes
/
Copy pathDP(Basic).cpp
File metadata and controls
34 lines (31 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<bits/stdc++.h>
using namespace std;
string arr,brr;
int n,m;
int dp[2005][2005];
int func(int i,int j)
{
if(i==0)
return j;
if(j==0)
return i;
if(dp[i][j]!=-1)
return dp[i][j];
if(arr[i-1]==brr[j-1])
return dp[i][j]=func(i-1,j-1);
else
return dp[i][j]=1+min(func(i-1,j),min(func(i,j-1),func(i-1,j-1)));
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(dp,-1,sizeof(dp));
cin>>arr>>brr;
n=arr.length();
m=brr.length();
cout<<func(n,m)<<endl;
}
}