首页
IT
登录
6mi
u
盘
搜
搜 索
IT
最长公共子序列lcs
最长公共子序列lcs
xiaoxiao
2021-04-16
26
#include<iostream>
#include
<cstring>
using
namespace
std;
string
z,s1,s2;
int
dp[
1010
][
1010
],l1,l2;
int
main() { cin
>>s1>>
s2; l1
=s1.length(),l2=
s2.length();
for
(
int
i=
1
;i<=l1;i++
)
for
(
int
j=
1
;j<=l2;j++
)
if
(s1[i-
1
]==s2[j-
1
])dp[i][j]=dp[i-
1
][j-
1
]+
1
;
else
dp[i][j]=max(dp[i-
1
][j],dp[i][j-
1
]);
int
i=l1,j=
l2;
while
(i&&
j) {
if
(s1[i-
1
]==s2[j-
1
]) { z
=s1[--i]+
z; j
--
; }
else
if
(dp[i-
1
][j]>dp[i][j-
1
])i--
;
else
j--
; } cout
<<
z; }
转载请注明原文地址: https://ju.6miu.com/read-672106.html
技术
最新回复
(
0
)