-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCSLenth2.cpp
101 lines (101 loc) · 2.14 KB
/
LCSLenth2.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*************************************************************************
> File Name: LCSLenth2.cpp
> Author:
> Mail:
> Created Time: 2017年04月13日 星期四 23时00分31秒
************************************************************************/
//递归版本实现最长子序的查找
#include<iostream>
using namespace std;
#define Max 100
static int c[Max][Max] = {{-1}};
static int b[Max][Max] = {{-1}};
void Found(char xx[],int i,int j)//从表的最后一个元素开始寻找1的元素,进而找最长子序
{
if(i == 0 || j == 0)
return;
if(b[i][j] == 1)
{
Found(xx,i-1,j-1);
cout << xx[i-1];
}
else if(b[i][j] == 2)
{
Found(xx,i,j-1);
}
else
{
Found(xx,i-1,j);
}
}
int LCSLen(char xx[],char yy[],int i,int j)
{
int tmp;
if(c[i][j] > 0)
return c[i][j];
if(i == 0 ||j == 0)
{
return 0;
}
else
{
if(xx[i-1] == yy[j-1])
{
c[i][j] = LCSLen(xx,yy,i-1,j-1) + 1;
b[i][j] = 1;
}
else if(LCSLen(xx,yy,i,j-1) > LCSLen(xx,yy,i-1,j))
{
c[i][j] = LCSLen(xx,yy,i,j-1);
b[i][j] = 2;//取左方
}
else
{
c[i][j] = LCSLen(xx,yy,i-1,j);
b[i][j] = 3;
}
}
return c[i][j];
}
int LCSLength(char xx[],char yy[],int mm,int nn)
{
int i,j;
for(i = 0; i <= mm; i++)
{
for(j = 0; j <= nn; j++)
{
c[i][j] = 0;
}
}
return LCSLen(xx,yy,mm,nn);
}
int main()
{
int m,n,i,j;
int total;
char x[Max],y[Max];
cout << "请输入X的和y的序列长度:"<< endl;
cin>>m>>n;
cout << "请输入x序列和y序列:"<<endl;
for(i = 0; i < m; i++)
{
cin >> x[i];
}
for(i = 0; i < n; i++)
{
cin >> y[i];
}
total = LCSLength(x,y,m,n);
for(i = 0; i <= m; i++)
{
for(j = 0; j <= n; j++)
{
cout << c[i][j] << ' ';
}
cout << endl;
}
cout << "最长子序列长度为:" << total;
cout << "最长公共子序列是:"<<endl;
Found(x,m,n);
return 0;
}