Topic Link: Click to enter
First, for the shortest length, it is easy to determine the length of the two strings and subtract their longest common subsequence length. Then the trouble is that the number of strings that fit the requirements, in fact, we can also use a similar to the longest common sub-sequence of the DP to seek.
Set Dp[i][j] Represents the str1 of the first I characters and str2 of the first J characters obtained by satisfying the required string, if STR[I]==STR[J], then dp[i][j]+=dp[i-1][j-1]; Otherwise, we will discuss the length of the longest common subsequence in these two locations, see the code for the I,j.
The code is as follows:
#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intmaxn= +;intDP1[MAXN][MAXN];Long LongDP2[MAXN][MAXN];CharSTR1[MAXN],STR2[MAXN];intMain () {//Freopen ("In.txt", "R", stdin); intT,case=0;scanf("%d%*c", &t); while(t--) {Gets (str1+1); Gets (str2+1);intlen1=strlen(str1+1);intLen2=strlen(str2+1);memset(DP1,0,sizeof(DP1));memset(DP2,0,sizeof(DP2)); for(intI=0; i<=len1;i++) dp2[i][0]=1; for(intI=0; i<=len2;i++) dp2[0][i]=1; for(intI=1; i<=len1;i++) for(intj=1; j<=len2;j++) {if(Str1[i]==str2[j]) {dp1[i][j]=dp1[i-1][j-1]+1; dp2[i][j]+=dp2[i-1][j-1]; }Else{Dp1[i][j]=max (dp1[i-1][j],dp1[i][j-1]);if(dp1[i][j]==dp1[i-1][J])/// These two situations are not oppositesdp2[i][j]+=dp2[i-1][J];if(dp1[i][j]==dp1[i][j-1]) dp2[i][j]+=dp2[i][j-1]; } }printf("Case #%d:%d%lld\n", ++case,len1+len2-dp1[len1][len2],dp2[len1][len2]); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10723--cyborg genes+ Longest common sub-sequence variants