Original question:
It ' s all true. But we still has a chance; Only if you can help with your math skills. You see, the blueprint of a cyborg DNA is complicated. The human DNA could is expressed by the arrangement of A (adenine), T (thiamine), G (guanine) C (cytosine) only. But for the cyborgs, it can is anything from
A to X. But that had made the problem only five folds more complicated. Its their ability to synthesize, DNAs from, different cyborgs to create another with all the quality of the parent T Hat gives us the shriek. We came to know that the relative ordering of the A, B, C, ..., X in A cyborg gene is crucial. A cyborg with a gene "ABAAXGF" was quite different from the one with "AABXFGA". They synthesize the genes from the cyborgs, the relative order of these elements in both the parents have to be Mai ntained. To construct a gene by joining the genes of the parents could has been very simple if we could put the structure from the First parent just before the structure of the second parent. But the longer the structure gets, the harder it gets to create a cyborg from that structure. The cyborgs has found a cost effective a-doing this synthesis. Their resultant genes is of the shortest possible
Length. For example, they could combine "abaaxgf" and "AABXFGA" to form "AABAAXGFGA". But thats is only one of the cyborgs that can is created from these genes. This ' cost effective synthesis ' can be do in many and other ways. We require you to find the shortest length of the gene structure that maintains the relative ordering of the elements in T He and the parent genes. You is also required to count the number of a unique cyborgs that can is created from these the parents. The cyborgs is different when their gene structures differ on least one place.
Input
The first line of the input gives you the number of test cases, T (1≤t≤15). Then T test cases follow. Each of the test cases consists of lines. The first line would give your the gene structure of the first parent, and the second line would give the structure of The second parent. These structures
is represented by strings constructed from the alphabet ' A ' to ' X '. You can assume this length of these strings does not exceed.
Output
For each of the test cases, you need to print one line of output. The output for each test case starts
With the test case number, followed by the shortest length of the gene structure and the number of
Unique cyborgs that can is created from the parent cyborgs. Can assume that the number of new
Cyborgs is always being less than 2 32. Look at the sample output for the exact format.
Illustration:the First test case is illustrated below:
Sample Input
3
Abaaxgf
Aabxfga
ABA
Bxa
Aabba
Bbabaa
Sample Output
Case #1:10 9
Case #2:4 1
Case #3:8 10
English:
Give you two strings that let you find a string s, the string S "contains" the two strings, which can be called the shortest common sequence (note not a subsequence). Then output the length of the sequence, and then output how many of this sequence is at this shortest length.
Note that there are empty strings to be used with getline or gets
#include <bits/stdc++.h> using namespace std;
String s1,s2;
int dp[35][35];
int cnt; void Dfs (int i,int j) {if (i==0| |
j==0) {cnt++;
Return
} if (S1[i-1]==s2[j-1]) {DFS (i-1,j-1);
} else {if (dp[i-1][j]==dp[i][j-1]) {DFS (I-1,J);
DFS (I,J-1);
} else {if (dp[i-1][j]>dp[i][j-1]) DFS (I,J-1);
else Dfs (I-1,J);
}}} int main () {Ios::sync_with_stdio (false);
int t,m=1;
cin>>t;
Cin.get ();
while (t--) {getline (CIN,S1);
Getline (CIN,S2);
cin>>s1>>s2;
for (int i=0;i<=s1.size (), i++) {for (int j=0;j<=s2.size (); j + +) Dp[i][j]=int_max;
} cnt=0;
dp[0][0]=0;
for (int i=1;i<=s1.size (); i++) {dp[i][0]=i; } for (int j=1;j<=s2. Size (); j + +) {dp[0][j]=j;
} for (int i=1;i<=s1.size (), i++) {for (int j=1;j<=s2.size (); j + +) {
if (S1[i-1]==s2[j-1]) {dp[i][j]=dp[i-1][j-1]+1;
} else {dp[i][j]=min (dp[i-1][j],dp[i][j-1]) +1;
}}} dfs (S1.size (), s2.size ());
cout<< "Case #" <<m++<< ":" <<dp[s1.size ()][s2.size ()]<< "" <<cnt<<endl;
} return 0;
}
Answer:
A relatively simple dynamic programming problem, because it is a result state of two strings expressed, you can set the state to
DP[I][J] Dp[i][j]
Represents the first string S1 and the second string S2, the shortest common sequence that is composed of the first and first J characters in length, respectively.
Then the state transfer equation can be written
dp[i][j]=
{
dp[i-1][j-1]+1 (when S1[i]==s2[j]
min (dp[i-1][j],dp[i][j-1]) +1 (when s1[i]!=s2[j])
}
It can be understood that if the characters s[i] and s2[j] are the same, then the current state only needs to add one more of the same character in the previous state.
Otherwise, find the short state, plus the short one.
(You can see it by drawing a two-dimensional table yourself)
Finally, using DFS to reverse the state transfer equation, find the path number is the answer.