uva 10723 Cyborg Genes

來源:互聯網
上載者:User

原題:
It’s all true. But we still have 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 be expressed by the arrangement of A (Adenine), T (Thiamine), G (Guanine) C (Cytosine) only. But for the cyborgs, it can be anything from
A to X. But that has made the problem only five folds more complicated. Its their ability to synthesize two DNAs from two different cyborgs to create another with all the quality of the parent that 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” is quite different from the one with“AABXFGA”. So when they synthesize the genes from two cyborgs, the relative order of these elements in both the parents has to be maintained. To construct a gene by joining the genes of the parents could have 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 have found a cost effective way of doing this synthesis. Their resultant genes are of the shortest possible
length. For example, they could combine “ABAAXGF” and“AABXFGA” to form “AABAAXGFGA”. But thats only one of the cyborgs that can be created from these genes. This“cost effective synthesis” can be done in many other ways. We require you to find the shortest length of the gene structure that maintains the relative ordering of the elements in the two parent genes. You are also required to count the number of unique cyborgs that can be created from these two parents. Two cyborgs are different when their gene structures differ in at 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 two lines. The first line would give you the gene structure of the first parent, and the second line would give you the structure of the second parent. These structures
are represented by strings constructed from the alphabet ‘A’ to ‘X’. You can assume that the length of these strings does not exceed 30 characters.
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 be created from the parent cyborgs. You can assume that the number of new
cyborgs will always be 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

中文:

給你兩個字串,讓你找出一個字串s,字串s“包含”這兩個字串,可以稱為最短公用序列(注意不是子序列)。然後輸出這個序列的長度,再輸出這個序列在這個最短的長度下有多少種。

注意,有空串,要用getline或者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;}

解答:

比較簡單的一道動態規劃問題,由於是兩個字串表達出的結果狀態,所以,可以設定狀態為

dp[i][j] dp[i][j]

表示第一個字串s1和第二個字串s2,在長度分別為前i個字元和前j個字元時組成的最短公用序列是多少。
那麼狀態轉移方程可以寫成

dp[i][j]={    dp[i-1][j-1]+1  (當s1[i]==s2[j]時)    min(dp[i-1][j],dp[i][j-1])+1  (當s1[i]!=s2[j]時)}

可以理解為,如果字元s[i]和s2[j]相同,那麼目前狀態只需要在上一個狀態多加上一個相同的字元即可。
否則,找到短的那一個狀態,加上短的那個字元即可。
(自己畫一個二維表格就能看出來)

最後,用dfs反推狀態轉移方程,尋找路徑數目即為答案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.