/
BOJ2602.cpp
77 lines (57 loc) · 1.07 KB
/
BOJ2602.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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string>
using namespace std;
#define MAX_N 20
#define MAX_M 100
#define INF 2000000000
#define MOD
//N : length of duru
//M : length of doldari
int N,M;
char duru[MAX_N + 2];
char doldari[2][MAX_M + 2];
long long int cache[MAX_N + 20][MAX_M + 20][2];
long long int dp(int n, int m, int turn)
{
if (n == N)
return 1;
if (m == M)
return 0;
long long int &ret = cache[n][m+1][turn];
if (ret != -1)
return ret;
ret = 0;
for (int i = m+1; i < M; i++)
{
if (duru[n] == doldari[turn][i])
ret += dp(n + 1, i, (turn+1)%2);
}
return ret;
}
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++)
{
memset(cache, -1, sizeof(cache));
cin >> duru >> doldari[0] >> doldari[1];
N = strlen(duru);
M = strlen(doldari[0]);
if (N > M)
{
cout << 0;
return 0;
}
cout << dp(0,-1,0) + dp(0, -1, 1);
}
return 0;
}