-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
43 lines (34 loc) · 824 Bytes
/
D.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
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
void run_cases() {
int n, m;
cin >> n >> m;
string A, B;
cin >> A >> B;
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
int ans = 0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(A[i-1] == B[j-1]) {
dp[i][j] = max(dp[i-1][j-1] + 2, dp[i][j]);
} else {
dp[i][j] = max(0, max(dp[i-1][j], dp[i][j-1]) - 1);
}
}
}
for(int i=0;i<=n;i++) {
for(int j=0;j<=m;j++) {
ans = max(ans, dp[i][j]);
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr);
int tests = 1;
// cin >> tests;
for(int test = 1;test <= tests;test++) {
run_cases();
}
}