Catching Cheaters
链接 : http://codeforces.com/problemset/problem/1446/B
最长公共子序列的变式
代码
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define mod 1e9 + 7
const int N = 5010;
char a[N], b[N];
int f[N][N];
int main() {
IO;
int n, m, ans = 0;
cin >> n >> m;
cin >> a + 1 >> b + 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (a[i] == b[j]) f[i][j] = f[i - 1][j - 1] + 2;
else {
f[i][j] = max(f[i - 1][j], f[i][j - 1]) - 1;
f[i][j] = max(0, f[i][j]);
}
ans = max(ans, f[i][j]);
}
cout << ans << endl;
return 0;
}