/
BOJ9251.cpp
92 lines (71 loc) · 1.21 KB
/
BOJ9251.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string>
using namespace std;
#define MAX_N 1000
int N;
int cache[MAX_N + 10][MAX_N + 10];
int choice[MAX_N + 10];
string A, B;
int lenA, lenB;
int solve(int a, int b)
{
if (a == lenA || b == lenB)
return 0;
int &ret = cache[a][b];
if (ret != -1)
return ret;
ret = 0;
int aBestNext = -1;
if (A[a] == B[b])
{
ret = 1 + solve(a + 1, b + 1);
}
else
{
ret = max(solve(a, b + 1), solve(a + 1, b));
}
choice[a] = aBestNext;
return ret;
}
void reconstruct(int a, int b, string& s)
{
if (a == lenA || b == lenB)
return;
if (A[a] == B[b])
{
s += A[a];
reconstruct(a + 1, b + 1, s);
}
else
{
if (cache[a][b] == cache[a + 1][b])
reconstruct(a + 1, b, s);
else
reconstruct(a, b + 1, s);
}
}
int main(int argc, char** argv)
{
int tc, T;
std::ios::sync_with_stdio(false);
#ifdef _WIN32
freopen("Text.txt", "r", stdin);
#endif
//cin >> T;
T = 1;
for (tc = 0; tc < T; tc++)
{
cin >> A >> B;
lenA = A.length();
lenB = B.length();
memset(cache, -1, sizeof(cache));
//string s = "";
cout << solve(0, 0) << endl;
//reconstruct(0, 0, s);
//cout << s;
}
return 0;
}