Rescue the Rabbit HDU4057

来源:互联网 发布:多益网络用户中心 编辑:程序博客网 时间:2024/06/16 22:49

题目描述:

Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, and Dr. X wants to take some rabbits to Noah’s Ark, or there are no rabbits any more.

A rabbit’s genes can be expressed as a string whose length is l (1 ≤ l ≤ 100) containing only ‘A’, ‘G’, ‘T’, ‘C’. There is no doubt that Dr. X had a in-depth research on the rabbits’ genes. He found that if a rabbit gene contained a particular gene segment, we could consider it as a good rabbit, or sometimes a bad rabbit. And we use a value W to measure this index.

We can make a example, if a rabbit has gene segment “ATG”, its W would plus 4; and if has gene segment “TGC”, its W plus -3. So if a rabbit’s gene string is “ATGC”, its W is 1 due to ATGC contains both “ATG”(+4) and “TGC”(-3). And if another rabbit’s gene string is “ATGATG”, its W is 4 due to one gene segment can be calculate only once.

Because there are enough rabbits on Earth before 2012, so we can assume we can get any genes with different structure. Now Dr. X want to find a rabbit whose gene has highest W value. There are so many different genes with length l, and Dr. X is not good at programming, can you help him to figure out the W value of the best rabbit.

输入输出:

Input
There are multiple test cases. For each case the first line is two integers n (1 ≤ n ≤ 10),l (1 ≤ l ≤ 100), indicating the number of the particular gene segment and the length of rabbits’ genes.

The next n lines each line contains a string DNAi and an integer wi (|wi| ≤ 100), indicating this gene segment and the value it can contribute to a rabbit’s W.

Output
For each test case, output an integer indicating the W value of the best rabbit. If we found this value is negative, you should output “No Rabbit after 2012!”.

Sample Input

2 4ATG 4TGC -31 6TGC 44 1A -1T -2G -3C -4 

Sample Output

44No Rabbit after 2012!

Hint
case 1:we can find a rabbit whose gene string is ATGG(4), or ATGA(4) etc. case 2:we can find a rabbit whose gene string is TGCTGC(4), or TGCCCC(4) etc. case 3:any gene string whose length is 1 has a negative W.

题目分析:

此题大意是l长度的一个母串中,有n个价值不等的模式串,其价值有可能为正也有可能为负,要求你求出母串中能够取得的最大价值是多少,若最大价值为负,则输出No Rabbit after 2012!(每个模式串最多计算一次)
这道题是AC自动机与状态压缩DP的综合,思路类似与HDU2825

代码如下:

#include <iostream>#include <stdio.h>#include <cstring>#include <algorithm>#include <stdlib.h>#include <vector>#include <math.h>#include <queue>#include <set>#include <string.h>using namespace std;#define N 1005#define INF 0xfffffffchar c[115];int v[12];int dp[2][N][1<<10];//dp[i][j][k]表示长度i的字符串,Tire树上j状态下,模式串k状态下的取值struct Tire{    int next[N][4],fail[N],end[N];//只可能有AGTC这四种    int L;    int id[128];    void init()    {        fail[0]=0;        id['A']=0,id['G']=1,id['T']=2,id['C']=3;        memset(next[0],0,sizeof(next[0]));        memset(end,0,sizeof(end));        L=1;    }    void insert(char *chr,int key)    {        int now=0;        for(;*chr; chr++)        {            if(!next[now][id[*chr]])            {                memset(next[L],0,sizeof(next[L]));                next[now][id[*chr]]=L++;            }            now=next[now][id[*chr]];        }        end[now]=(1<<key);    }    void buildfail()    {        int Q[N];        int head=0,tail=0;        for(int i=0; i<4; i++)        {            if(next[0][i])            {                fail[next[0][i]] = 0;                Q[tail++] = next[0][i];            }        }        while(head!=tail)        {            int u = Q[head++];            end[u]|=end[fail[u]];            for(int i = 0; i < 4; i++)            {                if(next[u][i])                {                    fail[next[u][i]] = next[fail[u]][i];                    Q[tail++] = next[u][i];                }                else next[u][i] = next[fail[u]][i];            }        }    }    void DP(int n,int m)    {        memset(dp,0,sizeof(dp));        dp[0][0][0] = 1;        for(int i=0 ; i<n ; i++)        {            memset(dp[(i+1)%2],0,sizeof(dp[(i+1)%2]));            for(int j=0 ; j<L ; j++)            {                for(int t=0 ; t<(1<<m) ; t++)                {                    if(!dp[i%2][j][t]) continue;                    for(int k=0 ; k<4; k++)                    {                        int tmp=end[next[j][k]];                        dp[(i+1)%2][next[j][k]][t|tmp] = dp[i%2][j][t];//类似的状态转移方程                    }                }            }        }        int cnt=-INF;        for(int i=0; i<L; i++)        {            for(int j=0; j<(1<<m); j++)            {                int ans = 0;                if(!dp[n%2][i][j]) continue;                for(int k=0; k<m; k++)                {                    if(j&(1<<k))                        ans+=v[k];                }                cnt = max(ans,cnt);//计算最终价值            }        }        if(cnt<0)            puts("No Rabbit after 2012!");        else            printf("%d\n",cnt);    }} ac;int main(){    int n,m;    while(scanf("%d%d",&m,&n)!=EOF)    {        ac.init();        for(int i=0 ; i<m ; i++)        {            scanf("%s%d",c,&v[i]);            ac.insert(c,i);        }        ac.buildfail();        ac.DP(n,m);    }    return 0;}
0 0