-
Notifications
You must be signed in to change notification settings - Fork 2
/
1013 - Love Calculator.cpp
54 lines (54 loc) · 1.2 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
#include<bits/stdc++.h>
using namespace std;
#define N 32
#define ll long long
int lcs[N][N];
ll dp[N][N][N*2];
char str1[N], str2[N];
int l1,l2,len;
int findLCS(int i,int j)
{
if(i<0 || j<0) return 0;
if(lcs[i][j]!=-1) return lcs[i][j];
int ret = 0;
if(str1[i]==str2[j]){
ret = findLCS(i-1, j-1) + 1;
}
else{
ret = max(findLCS(i-1, j), findLCS(i, j-1));
}
return lcs[i][j] = ret;
}
ll call(int i, int j, int l)
{
if(i>l1 || j>l2) return 0;
if(l==len){
if(i==l1 && j==l2) return 1;
else return 0;
}
if(dp[i][j][l]!=-1) return dp[i][j][l];
ll ret = 0;
if(str1[i]==str2[j]){
ret = call(i+1, j+1, l+1);
}
else{
ret = (call(i+1, j, l+1)) + (call(i, j+1, l+1));
}
return dp[i][j][l] = ret;
}
int main()
{
int tc,cs=1;
scanf("%d",&tc);
while(tc--){
memset(lcs,-1,sizeof lcs);
memset(dp, -1, sizeof dp);
scanf("%s%s",str1,str2);
l1 = strlen(str1);
l2 = strlen(str2);
len = l1+l2-findLCS(l1-1, l2-1);
ll ans = call(0,0, 0);
printf("Case %d: %d %lld\n",cs++,len,ans);
}
return 0;
}