-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1013.cpp
More file actions
88 lines (78 loc) · 2.01 KB
/
Copy path1013.cpp
File metadata and controls
88 lines (78 loc) · 2.01 KB
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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
//==================[ START ]=====================
typedef unsigned long long LLU;
typedef long long LL;
#define ft first
#define sd second
#define mp make_pair
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mem(a, b) memset(a, b, sizeof(a))
#define meminf(a) memset(a, 126, sizeof(a))
#define LL long long
#define LLU unsigned long long
#define inf 1e9
#define eps 1e-9
#define NN 1010
//==================[ END ]=====================
LL dp[35][35];
LL dlcs[35][35];
string s1, s2;
LL lcs(int i, int j) {
if (i <= 0 || j <= 0) return 0;
LL& tc = dlcs[i][j];
if (tc != -1) return tc;
if (s1[i] == s2[j])
tc = lcs(i - 1, j - 1) + 1;
else {
LL res = lcs(i - 1, j);
tc = max(res, lcs(i, j - 1));
}
return tc;
}
LL rec(int i, int j) {
if (i <= 0 || j <= 0) return 1;
LL& tc = dp[i][j];
if (tc != -1) return tc;
if (s1[i] == s2[j])
tc = rec(i - 1, j - 1);
else if (lcs(i - 1, j) > lcs(i, j - 1))
tc = rec(i - 1, j);
else if (lcs(i - 1, j) < lcs(i, j - 1))
tc = rec(i, j - 1);
else
tc = rec(i - 1, j) + rec(i, j - 1);
return tc;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1, tc;
cin >> tc;
LL i, j, k, l, m, n;
LL r, q;
while (tc--) {
cin >> s1;
cin >> s2;
s1 = " " + s1;
s2 = " " + s2;
mem(dp, -1);
mem(dlcs, -1);
LL res = s1.size() + s2.size() - 2;
res -= lcs(s1.size() - 1, s2.size() - 1);
LL ret = rec(s1.size() - 1, s2.size() - 1);
printf("Case %d: %lld %lld\n", t++, res, ret);
}
return 0;
}