TOJ 2641 Gene

描述

How can millions of different and complex structures be built using only a few simple building blocks? Just ask your DNA. DNA (short for deoxyribonucleic acid) spells out the genetic codes of millions of species, using just four molecules: adenine (A), guanine (G), thymine (T), and cytosine (C). These molecules are called nucleotide bases. Different sequences of nucleotide bases are what define each species.
Within this coil of DNA lies all the information needed to produce everything in the human body. A strand of DNA may be millions, or billions, of base-pairs long. Different segments of the DNA molecule code for different characteristics in the body. A Gene is a relatively small segment of DNA that codes for the synthesis of a specific protein. This protein then will play a structural or functional role in the body. A chromosome is a larger collection of DNA that contains many genes and the support proteins needed to control these genes.


Now , we give you the Sequence of some genes, which code for different proteins. And then we give every protein a score.here comes your problem,if we give you a chromosome in sequence , of course it can code many proteins, can you arrange which proteins it code , with the object to get The largest score (two proteins can not be overlap).

输入

There will be several testcases. For each testcase, the first line is an integer N(1 < N ≤ 100,000), the number of the genes we will give you. Then followed N lines , each line contains a string S(the lenth of S is no more than 10), the sequence of the gene , and an integer C, the score of the protein the gene code. The last line of each testcase is a string (the length of this string is no more than 1000), describes the sequence of the chromosome.

输出

For each testcase , output the largest score in the sample’s format.

样例输入

4
AATG 3
AT 2
GCGG 3
GG 2
AATGCGG
3
A 1
C 1
G 1
T

样例输出

Case 1: 5
Case 2: 0

题目来源

HNU 2009

dp[i+l]=max(dp[i]+M[temp]),M[temp]表示某基因的score值。

大量输入输出使用scanf。

#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int t,c=0;
    int dp[1001];
    while(scanf("%d",&t)!=EOF){
        char gene[20];
        int score;
        map< string,int > M;
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=t; i++){
            scanf("%s %d",gene,&score);
            M[gene]=score;
        }
        string g;
        cin>>g;
        int len=g.size();
        for(int i=0; i<len; i++){
            for(int l=1; l<=10; l++){
                if(l+i>len)continue;
                string temp=g.substr(i,l);
                if(dp[i+l] < dp[i]+M[temp]){
                    dp[i+l]=dp[i]+M[temp];
                }
            }
        }
        int ans=-1;
        for(int i=1; i<=len; i++){
            if(dp[i]>ans)
                ans=dp[i];
        }
        printf("Case %d: %d
",++c,ans);
    }        
    return 0;
}
原文地址:https://www.cnblogs.com/chenjianxiang/p/3545793.html