zoj 3545 - Rescue the Rabbit(AC自动机+dp)

Rescue the Rabbit

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!

dp[i][j][k]表示长度为i,位于节点j,状态为k的情况是否能够到达

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1010;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const int SIGMA_SIZE=4;
int N,L;
char s[maxn];
int W[maxn];
int dp[2][1100][1<<10];
struct AC
{
    int ch[maxn][SIGMA_SIZE];
    int fail[maxn],val[maxn];
    int sz;
    void clear()
    {
        memset(ch[0],0,sizeof(ch[0]));
        memset(val,0,sizeof(val));
        sz=1;
    }
    int idx(char x)
    {
        if(x=='A')return 0;
        if(x=='T')return 1;
        if(x=='G')return 2;
        return 3;
    }
    void insert(char *s,int id)
    {
        int u=0;
        for(int i=0;s[i];i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]|=(1<<id);
    }
    void getfail()
    {
        queue<int> q;
        int u=0;
        fail[u]=0;
        for(int c=0;c<SIGMA_SIZE;c++)
        {
            u=ch[0][c];
            if(u){fail[u]=0;q.push(u);}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            val[r]|=val[fail[r]];
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                u=ch[r][c];
                if(!u){ch[r][c]=ch[fail[r]][c];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][c])v=fail[v];
                fail[u]=ch[v][c];
            }
        }
    }
    int getval(int S)
    {
        int ans=0;
        for(int i=0;i<N;i++)
            if(S&(1<<i))ans+=W[i];
        return ans;
    }
    void solve()
    {
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=1;
        int cur=0;
        for(int i=0;i<L;i++)
        {
            memset(dp[cur^1],0,sizeof(dp[cur^1]));
            for(int j=0;j<sz;j++)
            {
                for(int p=0;p<(1<<N);p++)
                {
                    if(dp[cur][j][p]==0)continue;
                    for(int k=0;k<SIGMA_SIZE;k++)
                    {
                        int x=val[ch[j][k]];
                        dp[cur^1][ch[j][k]][p|x]=1;
                    }
                }
            }
            cur^=1;
        }
        int ans=-INF;
        for(int i=0;i<sz;i++)
        {
            int sum=0;
            for(int j=0;j<(1<<N);j++)
                if(dp[cur][i][j])
                    ans=max(ans,getval(j));
        }
        if(ans<0)printf("No Rabbit after 2012!\n");
        else printf("%d\n",ans);
    }
}ac;
int main()
{
    while(scanf("%d%d",&N,&L)!=EOF)
    {
        ac.clear();
        for(int i=0;i<N;i++)
        {
            scanf("%s%d",s,&W[i]);
            ac.insert(s,i);
        }
        ac.getfail();
        ac.solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值