-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2018-1-A.cpp
54 lines (42 loc) · 901 Bytes
/
2018-1-A.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
/*
将后续遍历逆序,问题等价为最长公共子序列的长度
*/
#include <iostream>
#include <string>
using namespace std;
const int N = 1000+5;
int dp[N][N];
string Reverse(string a){
string res="";
int len = a.size();
for(int i=len-1;i>=0;i--){
res += a[i];
}
return res;
}
int main()
{
string A,B;
while(cin>>A>>B){
int len = A.size();
for(int i=0;i<=len;i++)
dp[0][i]=dp[i][0]=0;
B = Reverse(B);
for(int i=1;i<=len;i++){
for(int j=1;j<=len;j++){
if(A[i-1]==B[j-1]){
dp[i][j] = dp[i-1][j-1]+1;
}
else{
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
cout<<dp[len][len]<<endl;
}
return 0;
}
/*
ABCDEFGH
CEFDBHGA
*/