最长公共子序列模型。
不同的解出现于d[i-1][j] == d[i][j-1]
不同的解出现于d[i-1][j] == d[i][j-1]
Run TIme: 0.009s
#define UVa "9-6.10723.cpp" //Cyborg Genes
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main() {
const int maxn = 30+5;
int T, n1, n2;
scanf("%d", &T);
getchar();
for(int kase = 1; kase <= T; kase ++) {
int d[maxn][maxn];
int cnt[maxn][maxn];
string str1, str2;
getline(cin, str1);
getline(cin, str2);
n1 = str1.length();
n2 = str2.length();
for(int i = 0; i <= n1; i ++) {
d[i][0] = 0;
cnt[i][0] = 1;
}
for(int j = 0; j <= n2; j ++) {
d[0][j] = 0;
cnt[0][j] = 1;
}
for(int i = 1; i <= n1; i ++) {
for(int j = 1; j <= n2; j ++) {
int& u = d[i][j], &uc = cnt[i][j];
if(str1[i-1] == str2[j-1]) {
u = d[i-1][j-1] + 1;
uc = cnt[i-1][j-1];
}
else {
int a = d[i-1][j], b = d[i][j-1];
u = max(a, b);
if(a > b)
uc = cnt[i-1][j];
else if(a < b)
uc = cnt[i][j-1];
else
uc = cnt[i-1][j] + cnt[i][j-1];
}
}
}
printf("Case #%d: %d %d\n", kase, n1+n2-d[n1][n2], cnt[n1][n2]);
}
return 0;
}