Cyborg Genes

时间:2021-08-02 14:46:56

题意:

给两个字符串,求最短的以两字符串为子序列的字符串和个数

分析:

最长公共子序列的变形,num[i][j]表示个数

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
char s1[],s2[];
int dp[][],num[][];
void solve(){
memset(dp,,sizeof(dp));
memset(num,,sizeof(num));
int l1=strlen(s1);
int l2=strlen(s2);
for(int i=;i<=l1;++i)
num[i][]=;
for(int i=;i<=l2;++i)
num[][i]=;
for(int i=;i<=l1;++i)
for(int j=;j<=l2;++j){
if(s1[i-]==s2[j-]){
dp[i][j]=dp[i-][j-]+;
num[i][j]=num[i-][j-];
}
else {
if(dp[i-][j]>dp[i][j-]){
dp[i][j]=dp[i-][j];
num[i][j]=num[i-][j];
}
else if(dp[i-][j]<dp[i][j-])
{
dp[i][j]=dp[i][j-];
num[i][j]=num[i][j-];
}
else {
dp[i][j]=dp[i-][j];
num[i][j]=num[i-][j]+num[i][j-];
}
}
}
printf("%d %d\n",l1+l2-dp[l1][l2],num[l1][l2]);
}
int main()
{
int t;
scanf("%d",&t);
getchar();
for(int c=;c<=t;++c)
{
gets(s1);
gets(s2);
printf("Case #%d: ",c);
solve();
}
return ;
}