Rescue the Rabbit HDU4057

帮助生物学家Dr. X寻找W值最高的兔子基因,通过分析DNA片段和它们对应的权重,解决AC自动机与状态压缩DP问题。
摘要由CSDN通过智能技术生成

题目描述:

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 4
ATG 4
TGC -3

1 6
TGC 4

4 1
A -1
T -2
G -3
C -4 

Sample Output

4
4
No 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 0xfffffff
char 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值