SRM 435 DNADeletion

题目:

PROBLEM STATEMENT

In genetics, a DNA sequence is a sequence of nucleotides (A, C, G or T).  Some DNA sequences translate to proteins, which are non-empty sequences of amino acids.  Let's examine the DNA translation process::

From left to right, split the DNA sequence into consecutive, non-overlapping triples of nucleotides.  Each triple is called a codon.  There may be one or two nucleotides left over at the end - those should be ignored.  For example, the DNA sequence "ACCTGTACG" will produce the codon sequence "ACC", "TGT", "ACG".  The DNA sequence "ACCTGTAC" will produce the codon sequence "ACC", "TGT" ("AC" is left over and ignored).
You are given a codon table that maps codons to their associated amino acids.  From left to right, look up each codon in the sequence generated above and replace it with its associated amino acid.  Every codon in the sequence must have an associated amino acid - otherwise, the DNA sequence does not translate to a protein.  For example, if "ACC" and "ACG" each map to threonin ("thr") and "TGT" maps to cysteine ("cys"), the DNA sequence "ACCTGTACG" will translate to the protein "thr cys thr".

Sometimes, after replication, one or more nucleotides in a DNA sequence go missing.  This situation is called deletion.  After a deletion, a DNA sequence can become any of its subsequences.  For example, "ACTG" may become "ACG" or "CG".

You are given a string[] DNASequence containing a DNA sequence.  Concatenate the elements of DNASequence to obtain the DNA sequence.  You are also given a string[] codonTable containing the codon table.  Each element is formatted "CODON AMINOACID" (quotes for clarity), where AMINOACID is the name of the amino acid associated with codon CODON.  Compute the number of different possible proteins that the given DNA sequence can translate to after undergoing zero or more deletions.  Since this number can be quite large, return its value modulo 1000000007.  Remember that only nonempty amino acid sequences are considered proteins.


DEFINITION
Class:DNADeletion
Method:differentProteins
Parameters:string[], string[]
Returns:int
Method signature:int differentProteins(string[] DNASequence, string[] codonTable)


CONSTRAINTS
-DNASequence will contain between 1 and 50 elements, inclusive.
-Every element of DNASequence will contain between 1 and 50 characters, inclusive.
-Every element of DNASequence will contain only characters 'A', 'C', 'T', 'G'.
-codonTable will contain between 1 and 50 elements, inclusive.
-Every element of codonTable will contain a codon, followed by a single space, followed by an amino acid.
-Every codon in codonTable will contain exactly 3 characters.
-Every codon in codonTable will contain only characters 'A', 'C', 'T', 'G'.
-Every codon in codonTable will be unique.
-Every amino acid in codonTable will contain between 1 and 20 characters.
-Every amino acid in codonTable will contain only letters ('a'-'z', 'A'-'Z').


EXAMPLES

0)
{"ACTG"}
{"ACT gua", "ACG cys", "ATG leu", "CTG thr"}

Returns: 4

You can get proteins:
"gua" (deletion of 'G' or no deletion),
"cys" (deletion of 'T'),
"leu" (deletion of 'C'),
"thr" (deletion of 'A').
Other deletions do not result in proteins.

1)
{"AAACCC"}
{"AAA thr", "CCC cys"}

Returns: 3

You can get proteins: "thr", "cys" and "thr cys".

2)
{"AAATCCC"}
{"AAA gua","TCC dop","AAT dop","CCC gua"}

Returns: 5

You can get proteins:
"gua",
"dop",
"gua dop" (from sequence "AAATCC"),
"dop gua" (from sequence "AATCCC"),
"gua gua" (from sequence "AAACCC").

3)
{"ATGCGCATTAACCTCCTACCATGGAAGGGACGTAACCCGGCAATTTGATC",
 "CTGATGACGGCATAAGCTACCCCTAGAGGTAAAAATGCATACTGCGTGCT",
 "ATGCAG"}
{"AAC RpjZt","AAT ZeiC","GCA ChZwh","TCC RpjZt","GAA I",
 "TAG ZeiC","CTG dVK","GAG ZeiC","GTG I","AAG q","ATT dVK",
 "AGA cJEjM","GGG KONUd","GTC ZRV","GGC ZeiC","TTA KONUd",
 "GAC q","CCA q","GCC ZRV","GCG RpjZt","CCT ZRV","ATG dVK",
 "ATC ChZwh","CTC cJEjM","CCC q","ATA dWjz","TTG DkEG",
 "CAG q","CAA ZRV","ACT dVK","TCG dVK","ACC I","CGC dVK"}

Returns: 455985264

Be sure to concatenate the elements of DNASequence.

-----------------------------------------------------------------------------------------------------

这个题目本来没有好的思路,然后看别人的代码。知道了应该用动态规划来做。

首先我们可以把所有的 DNASequence 连接在一起。因为这样不影响最后的结果。

然后对于一个长句子,我们发现他可以由前面的短句子加上最后一个单词组成。 得到动态转换方程。

然后注意到 要求是 转换的后的句子不能是相同的。也就是说 我们可以按照转换后的句子来尝试替换 如果可以 选择最长的短句子。也就是从后往前匹配。

 

代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
public class DNADeletion {
    
int mod = 1000000007;

    
public int differentProteins(string[] DNASequence, string[] codonTable) {
        
string s = "";
        
for (int i = 0; i < DNASequence.Length; i++)
            s 
+= DNASequence[i];

        Dictionary
<string, List<string>> paire = new Dictionary<string, List<string>>();
        
foreach (string ss in codonTable)
        {
            
string[] p = ss.Split(' ');
            
if(paire.ContainsKey(p[1])==false)
            {
                paire[p[
1]] = new List<string>();
            }

            paire[p[
1]].Add(p[0]);
        }

        
int n = s.Length;

        
int[] sum = new int[n + 1];
        sum[
0= 1;
        
for (int i = 1; i <= n; ++i)
        {
            sum[i]
=1;
            
foreach (KeyValuePair<string,List<string>> par in paire )
            {
                
int left = -1;
                
foreach (string code in par.Value)
                {
                    
int at = code.Length;
                    
int j = i;
    
                    
while (j>0&&at>0)
                    {
                        
--j;
                        
if (s[j] == code[at - 1])
                            
--at;
                    }
    
                    
if(at==0)
                    {
                        left 
= Math.Max(j, left);
                    }
                }
    
                
if(left>=0)
                {
                    sum[i] 
+= sum[left];
                    
if (sum[i] >= mod)
                        sum[i] 
-= mod;
                }
            }
        }

        
return (sum[n] + mod - 1% mod;
    }
}


转载于:https://www.cnblogs.com/cherip/archive/2009/02/15/1391133.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值