-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.cpp
More file actions
48 lines (46 loc) · 965 Bytes
/
Copy pathLCS.cpp
File metadata and controls
48 lines (46 loc) · 965 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
46
47
48
/*
[LCS]
[最長共用子序列]
*/
#include<bits/stdc++.h>
using namespace std;
#define INT long long int
string s,t;//輸入字串
INT dp[3001][3001];
void lcs(INT n,INT m){//n=s.size(),m=t.size() 求兩字串的最長共用子序列
for(INT i=1;i<=n;i++){
for(INT j=1;j<=m;j++){
if(s[i-1]==t[j-1]){
dp[i][j]=dp[i-1][j-1]+1;
}else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
/*void printdp(INT n,INT m){//DBG 印出dp表格
if(debug){
for(INT i=0;i<=n;i++){
for(INT j=0;j<=m;j++){
cout<<dp[i][j]<<",";
}
cout<<endl;
}
cout<<endl;
}
}*/
string backlcs(INT n,INT m){//回推最長共用子序列
string re="";
INT i=n,j=m;
while(i && j){
if(s[i-1]==t[j-1]){//如果兩字元相同,加入re,並往左上角移動
re.push_back(s[i-1]);
i--;
j--;
}else{
if(dp[i-1][j]>=dp[i][j-1]){//如果上面的數字比左邊大,往上移動
i--;
}else j--;
}
}
reverse(re.begin(),re.end());
return re;
}