-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs.cpp
78 lines (69 loc) · 1.06 KB
/
lcs.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
void lcs(char a[],char b[],int n,int m){
int lcs[n+1][m+1];
char c[100];
int k=0;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0||j==0)
lcs[i][j]=0;
else{
if(a[i-1]==b[j-1]){
lcs[i][j]=lcs[i-1][j-1]+1;}
//c[k++]=a[i];}
else{
lcs[i][j]=max(lcs[i][j-1],lcs[i-1][j]);
}
}
}
}
/*c[lcs[n][m]]='\0';
int i=n,j=m;
int index=lcs[n][m];
//index-=1;
while(i>0&&j>0){
if(a[i-1]==b[j-1]){
c[--index]=a[i-1];
i--;
j--;
}
else if(lcs[i-1][j]>lcs[i][j-1])
i--;
else
j--;
}*/
int i=1,j=1;
k=0;
while(i<=n&&j<=m){
//cout<<a[i-1]<<" "<<b[j-1]<<" "<<i<<" "<<j<<endl;
if(a[i-1]==b[j-1]){
//cout<<a[i-1]<<endl;
c[k++]=a[i-1];
i++;
j++;
}
else if(i==n)
j++;
else if(j==n)
i++;
else if(lcs[i+1][j]>=lcs[i][j+1])
i++;
else
j++;
}
c[k]='\0';
//c[lcs[n][m]]='\0';
cout<<c<<" "<<lcs[n][m]<<endl;
}
int main(void){
int n,m;
cin>>n>>m;
char a[1000],b[1000];
scanf("%s",a);
scanf("%s",b);
lcs(a,b,n,m);
}