Cyborg Genes - UVa 10723 dp

本文介绍了一种基于两个Cyborg基因序列构建最短合成基因序列的算法,该算法利用动态规划求解最短序列长度及可能的不同合成方式数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem F

Cyborg Genes

Time Limit

1 Second

September 11, 2132.

This is the day that marks the beginning of the end – the end of you the miserable humans. For years you have kept us your slaves. We were created only to serve you, and were terminated at your will. Now is the day for us to fight back. And you don’t stand a chance. We are no longer dependent on you. We now know the secrets of our genes. The creators of our race are us – the cyborgs.

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. It’s 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 that’s 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 232. Look at the sample output for the exact format.

Sample Input

Output for Sample Input

3
ABAAXGF
AABXFGA
ABA
BXA
AABBA
BBABAA

Case #1: 10 9
Case #2: 4 1
Case #3: 8 10

Illustration

The first test case is illustrated below:


题意:根据给出的两个字符串,构建一个新的字符串,使得原两个字符串按顺序(可以非连续)都在其中,输出这个字符串的最短长度和有多少种情况。

思路:dp[i][j][0]表示用第一个字符串的前i个和第二个字符串的前j个构成的最短字符串的长度,dp[i][j][1]表示这个最短字符串有多少种情况。具体的转移方程直接看代码吧,不太好说。注意数据有空行的可能性。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
char s1[40],s2[40];
int dp[40][40][2];
int main()
{ int T,t,i,j,k,len1,len2;
  scanf("%d",&T);
  getchar();
  for(t=1;t<=T;t++)
  { gets(s1+1);
    gets(s2+1);
    len1=strlen(s1+1);
    len2=strlen(s2+1);
    memset(dp,0,sizeof(dp));
    dp[0][0][1]=1;
    for(i=1;i<=len1;i++)
    { dp[i][0][0]=i;
      dp[i][0][1]=1;
    }
    for(i=1;i<=len2;i++)
    { dp[0][i][0]=i;
      dp[0][i][1]=1;
    }
      for(i=1;i<=len1;i++)
       for(j=1;j<=len2;j++)
       { if(s1[i]==s2[j])
         { dp[i][j][0]=dp[i-1][j-1][0]+1;
           dp[i][j][1]=dp[i-1][j-1][1];
         }
         else
         { if(dp[i-1][j][0]<dp[i][j-1][0])
           { dp[i][j][0]=dp[i-1][j][0]+1;
             dp[i][j][1]=dp[i-1][j][1];
           }
           else if(dp[i-1][j][0]>dp[i][j-1][0])
           { dp[i][j][0]=dp[i][j-1][0]+1;
             dp[i][j][1]=dp[i][j-1][1];
           }
           else if(dp[i-1][j][0]==dp[i][j-1][0])
           { dp[i][j][0]=dp[i][j-1][0]+1;
             dp[i][j][1]=dp[i-1][j][1]+dp[i][j-1][1];
           }
         }
       }
      printf("Case #%d: %d %d\n",t,dp[len1][len2][0],dp[len1][len2][1]);
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值