-
Notifications
You must be signed in to change notification settings - Fork 0
/
D. Catching Cheaters.cpp
56 lines (45 loc) · 1014 Bytes
/
D. Catching Cheaters.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
55
56
#include <fstream>
#include <string>
#include <iostream>
#include <math.h>
#include <algorithm>
#include<bits/stdc++.h>
#include <vector>
using namespace std;
int lcs( string X, string Y, int m, int n )
{
int l[m + 1][n + 1];
int i, j;
for (i = 0; i <= m; i++)
{
for (j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
l[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
l[i][j] = l[i - 1][j - 1] + 2;
else
l[i][j] = max(l[i - 1][j], l[i][j - 1]) - 1;
if (l[i][j] <= 0){
l[i][j] = 0;
}
}
}
int ma = 0;
for (int i = 0; i <= m; i++){
for (int j = 0; j <= n; j++){
ma = max(ma, l[i][j]);
}
}
return ma;
}
int main()
{
int n;
int m;
cin >> n >> m;
string a, b;
cin >> a >> b;
int ms = lcs( a, b, n, m );
cout << ms << "\n";
}