-
Notifications
You must be signed in to change notification settings - Fork 34
/
1013-Love-Calculator.cpp
94 lines (69 loc) · 1.31 KB
/
1013-Love-Calculator.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
93
94
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int n;
int m;
char a[100];
char b[100];
long long count;
unsigned long long ans2[40][40][40];
long long ans[40][40];
int temp;
unsigned long long explore2(int i, int j, int l)
{
if(i == n and j != m) {
return explore2(i, j+1, l-1);
}
if(j == m and i != n) {
return explore2(i+1, j, l-1);
}
if(i == n and j == m and l == 0) {
return 1;
}
if(i == n and j == m and l != 0) {
return 0;
}
if(ans2[i][j][l] != -1) {
return ans2[i][j][l];
}
if(a[i] == b[j]) {
return ans2[i][j][l] = explore2(i+1, j+1, l -1);
}
return ans2[i][j][l] = explore2(i+1, j, l -1) + explore2(i, j+1, l -1);
}
int explore(int i, int j)
{
if(i == n) {
count++;
return m - j;
}
if(j == m) {
count++;
return n - i;
}
if(ans[i][j] != -1) {
return ans[i][j];
}
if(a[i] == b[j]) {
return ans[i][j] = 1 + explore(i+1, j+1);
}
return ans[i][j] = 1 + min(explore(i+1, j),explore(i, j+1));
}
int main()
{
int t;
long long epic;
scanf("%d", &t);
for (int cs = 1; cs <= t ; cs++) {
scanf("%s", a);
scanf("%s", b);
n = strlen(a);
m = strlen(b);
memset(ans, -1, sizeof(ans));
temp = explore(0, 0);
memset(ans2, -1, sizeof(ans2));
epic = explore2(0, 0, temp);
printf("Case %d: %d %lld\n",cs,temp,epic);
}
}