hnu 11450 - nazgul's Blog

hnu 11450

nazgul posted @ 2010年10月18日 23:31 in HNU , 646 阅读

 

http://acm.hnu.cn/online/?action=problem&type=show&id=11450
用trie树记录所有可能的基因,然后用背包。对第i个背包时计算出从i开始可以扩展的所有j的值。
即f[i+len(gene)] = max{ f[i+len(gene)], f[i]+v[gene] ),gene是从i开始可以找到的基因。
 
	#include <stdio.h>
#include <string.h>

struct node
{
    int value;
    int nt[4];
}trie[289000];
int now;
int f[10110];
char s[10001];

int CharToInt( char c )
{
    switch ( c )
    {
    case 'A': return 0;
    case 'T': return 1;
    case 'C': return 2;
    case 'G': return 3;
    }
}

void Trie_Insert( char *s, int p )
{
    int k = 0, t;
    while ( *s )
    {
        t = CharToInt( *s );
        if ( trie[k].nt[t] == 0 )
        {
            trie[k].nt[t] = now++;
        }
        k = trie[k].nt[t];
        s++;
    }
    trie[k].value = p;
}

void Trie_Search( char *s, int l )
{
    int k = 0, i = l, t;
    while ( *s )
    {
        k = trie[k].nt[CharToInt( *s )];
        if ( k == 0 ) break;
        t = trie[k].value;
        if ( t )
        {
            if ( l == 0 )
            {
                if ( t > f[i] ) f[i] = t;
            }
            else
            if ( f[i] < t+f[l-1] )
                f[i] = f[l-1] + t;
        }
        i++; s++;
    }
}

int DynamicProg( char *s )
{
    int len = strlen( s ), i, Max = 0;
    for ( i=0; i<len; ++i )
    {
        Trie_Search( s+i, i );
        if ( i && f[i] < f[i-1] ) f[i] = f[i-1];
        if ( Max < f[i] ) Max = f[i];
    }
    return Max;
}

int main()
{
    int T = 1, p, n;

    while ( scanf( "%d", &n ) != EOF )
    {
        printf( "Case %d: ", T++ );
        memset( trie, 0, sizeof( trie ) );
        memset( f, 0, sizeof( f ) );
        now = 1;
        while ( n-- )
        {
            scanf( "%s%d", s, &p );
            Trie_Insert( s, p );
        }
        scanf( "%s", s );
        printf( "%d\n", DynamicProg( s ) );
    }
    return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee