-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLCS.cpp
More file actions
45 lines (45 loc) · 1011 Bytes
/
Copy pathLCS.cpp
File metadata and controls
45 lines (45 loc) · 1011 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
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
#include<string.h>
int s1[100][100];
int s2[100][100];
int LCS_Length(char x[100],char y[100]) {
int m=strlen(x);
int n=strlen(y);
int i,j;
memset(s1,0,sizeof(s1)); //根据递归方程的第一种情况,先初始化数组s1[][]
for(i=1; i<=m; i++)
for(j=1; j<=n; j++) {
if(x[i-1] == y[j-1]) {
s1[i][j]=s1[i-1][j-1]+1;
s2[i][j]=1;
} else if(s1[i-1][j] >= s1[i][j-1]) {
s1[i][j]=s1[i-1][j];
s2[i][j]=2;
} else {
s1[i][j]=s1[i][j-1];;
s2[i][j]=3;
}
}
return s1[m][n];
}
void Print_LCS(char X[100],int i,int j) { //输出最优解
if( (i == 0) || (j == 0) )
return;
if(s2[i][j] == 1) {
Print_LCS(X,i-1,j-1);
printf("%c",X[i-1]);
} else if(s2[i][j] == 2)
Print_LCS(X,i-1,j);
else
Print_LCS(X,i,j-1);
}
int main() {
char X[100],Y[100];
while(gets(X)&&gets(Y)) {
int p=LCS_Length(X,Y);
printf("这两个字符串的LCS为:%d\n",p);
printf("该公共子序列为:");
Print_LCS(X,strlen(X),strlen(Y));
}
return 0;
}