大意略。
思路:参考了网上的资料,自己漏掉了一种情况。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
const int MAXN = 40;
int d1[MAXN][MAXN];
int d2[MAXN][MAXN];
char str1[MAXN], str2[MAXN];
int n, m;
void dp()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(str1[i] == str2[j])
{
d1[i][j] = d1[i-1][j-1] + 1;
d2[i][j] = d2[i-1][j-1];
}
else
{
d1[i][j] = min(d1[i-1][j], d1[i][j-1]) + 1;
if(d1[i-1][j] < d1[i][j-1])
{
d2[i][j] = d2[i-1][j];
}
else if(d1[i-1][j] > d1[i][j-1])
{
d2[i][j] = d2[i][j-1];
}
else d2[i][j] = d2[i-1][j] + d2[i][j-1];
}
}
}
}
void read_case()
{
gets(str1+1);
gets(str2+1);
}
void solve()
{
read_case();
n = strlen(str1+1), m = strlen(str2+1);
for(int i = 0; i < MAXN; i++) d1[0][i] = d1[i][0] = i;
for(int i = 0; i < MAXN; i++) d2[0][i] = d2[i][0] = 1;
dp();
printf("%d %d\n", d1[n][m], d2[n][m]);
}
int main()
{
int T, times = 0;
scanf("%d%*c", &T);
while(T--)
{
printf("Case #%d: ", ++times);
solve();
}
return 0;
}