-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.txt
41 lines (36 loc) · 984 Bytes
/
1.txt
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
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
//taking input
string a,b;
cin>>a>>b;
int a_len=a.size(),b_len=b.size();
char lcs_word[min(a_len,b_len)];
//array for dp
int lcs[a_len+1][b_len+1];
//initialising the array with 0
memset(lcs,0, sizeof(lcs[0][0])*(a_len+1)*(b_len+1));
for(int i=0;i<a_len+1;i++){
for(int j=0;j<b_len+1;j++){
//cout<<lcs[i][j]<<" ";
}
//cout<<endl;
}
for(int i=1;i<=a_len;i++){
for(int j=1;j<=b_len;j++){
if(a[i-1]==b[j-1]){
lcs[i][j]=1+lcs[i-1][j-1];
}
else{
lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
}
}
}
cout<<"max length of LCS is "<<lcs[a_len][b_len]<<endl;
}
return 0;
}