Hdu 3341 Lost's revenge (字符串_AC自动机(DP))

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3341


题目大意:题目描述十分幽默,特别是Spring Brother,笑翻了。言归正传,题目给出n个数论DNA序列,含这些序列则可以增加数论水平,含几个就几级,再给出一个长为L的基因,问这个基因重组之后的最大数论水平是多少,如果一个DNA序列在基因中出现多次也要累加起来。


解题思路:这题乍一看,没什么思路,但膜拜了下Spring Brother,思路就来了。由于是重组,那么就可以抽象出这题的模型:用基因里的A个A,B个C,C个G,D个T组成一个长度为L的字符串,使得这个字符串含最多的数论DNA序列。想到模型之后,就可以想实现,如果我们暴力得一个一个枚举,那复杂度高得离谱。由于重组后长度为L的字符串是由长度为L-1的字符串改造而来,而长度为L-1的字符串是由长度L-2的字符串改造而来,以此类推,后面的结果不影响前面的,无后效性并且状态明显,想着用DP。

     设Dp[i][j]表示A个A,B个C,C个G,D个组成一个唯一的i(其实就是状态压缩),并且在AC自动机上的j位置的最高数论水平。那么状态转移方程为:

     dp[i+hash[x]][j] = max(dp[i+hash[x]][j],dp[i][j] + j的子节点的危险系数(即几个串在这里结束)(0 < j < total,0 < x < 4,hash[x]为1个x基因的hash值)。

     想出状态转移方程,就好办了。先将各个DNA碱基的数量压缩成一个数t,然后从0-t去遍历,再在trie图上进行状态转移。最坏复杂度为O(500*11^4*4)。     


测试数据:

2
AAA
AAG

AAAG    


2
A
TG

TGAATG


4
A
G
C
T
AGT


1
AA
AAA


1
AAA
AAA


1
A
ACT


代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) (a) > (b) ? (a) : (b)


struct node {

    int flag,in;
    node *next[5],*fail;
}*q[500010],trie[1050];
char sub[100],str[1010];
int  num[4],sum,hash[4];
int  total,head,tail,dp[15000][510];


inline int code (char c) {

    switch(c){

        case 'A': return 0;
        case 'C': return 1;
        case 'G': return 2;
        case 'T': return 3;
    }
    return 0;
}

inline node *new_node() {

    trie[total].in = total ;
    trie[total].flag = 0;
    trie[total].fail = NULL;
    for (int i = 0; i < 4; ++i)
        trie[total].next[i] = NULL;
    return &trie[total++];
}

void Insert(char *str,node *root) {

    int i = 0,k;
    node *p = root;


    while (str[i]) {

        k = code(str[i++]);
        if (p->next[k] == NULL) p->next[k] = new_node();
        p = p->next[k];
    }
    p->flag++;
}

void Auto_Ac(node *root) {

    root->fail = NULL;
    q[head++] = root;


    while (head != tail) {
        
        node *temp = q[tail++];        //获取头结点
        for (int i = 0; i < 4; ++i) {

            if (temp->next[i] != NULL) {
                if (temp == root) temp->next[i]->fail = root;
                else {

                    temp->next[i]->fail = temp->fail->next[i];
                    temp->next[i]->flag += temp->next[i]->fail->flag;
                }
                q[head++] = temp->next[i];
            }
            else {

                if (temp == root) temp->next[i] = root;
                else temp->next[i] = temp->fail->next[i];
            }
        }//for i
    }//while  !=
}

int Solve_1A() {

    int i,j,k,t,ans,ACTG[4];
    int A,B,C,D,hash[4];
    

    hash[3] = 1;
    hash[2] = hash[3] * (num[3] + 1);    //比前面一个碱基压缩成的数大
    hash[1] = hash[2] * (num[2] + 1);    //比前面二个碱基压缩成的数大
    hash[0] = hash[1] * (num[1] + 1);    //比前面三个碱基压缩成的数大
    t  = hash[0] * num[0] + hash[1] * num[1];
    t += hash[2] * num[2] + hash[3] * num[3];//压缩成的总和

    //初始化,ans设为0,最差的情况就是0了
    memset(dp,-1,sizeof(dp));
    ans = dp[0][0] = 0;
    

    for (i = 0; i <= t; ++i) {

        ACTG[0] = i / hash[0],ACTG[1] = (i % hash[0]) / hash[1];
        ACTG[2] = (i % hash[1]) / hash[2],ACTG[3] = i % hash[2]; //D = i % hash[2] / hash[3]
        if (ACTG[0]  > num[0] || ACTG[1] > num[1] 
            || ACTG[2] > num[2] || ACTG[3] > num[3]) continue;


        for (j = 0; j < total; ++j) if (dp[i][j] != -1){
    
            for (k = 0; k < 4; ++k) {

                node *p = trie[j].next[k];
                if (ACTG[k] < num[k] && dp[i+hash[k]][p->in] < dp[i][j] + p->flag) //A多一个,转移
                    dp[i+hash[k]][p->in] = dp[i][j] + p->flag;
            }
        }
    }


    for (i = 0; i < total; ++i)
        ans = max(ans,dp[t][i]);
    return ans;
}


int main()
{
    int i,j,k,n,cas = 0;


    while (scanf("%d",&n) ,n) {

        total = head = tail = 0;
        node *root = new_node();
        for (i = 1; i <= n; ++i)
            scanf("%s",sub),Insert(sub,root);


        Auto_Ac(root);
        scanf("%s",str);
        memset(num,0,sizeof(num));
        for (i = 0; str[i]; ++i) 
            num[code(str[i])] ++;
        printf("Case %d: %d\n",++cas,Solve_1A());
    }
}

本文ZeroClock原创,但可以转载,因为我们是兄弟。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值