• HDU 3341 Lost's revenge(AC自动机+DP)


    Lost's revenge

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 2262    Accepted Submission(s): 565


    Problem Description
    Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talent in the field of number theory. So Lost had never won the game. He was so ashamed and angry, but he didn't know how to improve his level of number theory.

    One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".

    It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
     
    Input
    There are less than 30 testcases.
    For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
    Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
    The last line is Lost's gene sequences, its length is also less or equal 40.
    All genes and gene sequences are only contains capital letter ACGT.
     
    Output
    For each testcase, output the case number(start with 1) and the most "level of number theory" with format like the sample output.
     
    Sample Input
    3 AC CG GT CGAT 1 AA AAA 0
     
    Sample Output
    Case 1: 3 Case 2: 2
     
    Author
    Qinz@XDU
     
    Source
     
    Recommend
    lcy
     
     
     
     
    字符最长是40
    只需要记录ACGT出现的次数。
     
    如果使用5维数组,显然超内存了。
     
    假设ACGT的总数分别为num[0],num[1],num[2],num[3]
     
    那么对于ACGT的数量分别为ABCD的状态可以记录为:
     
    A*(num[1]+1)*(num[2]+1)*(num[3]+1) + B*(num[2]+1)*(num[3]+1)+ C*(num[3]+1) +D
     
    这样的状态最大就是11*11*11*11
     
    复杂度也可以承受了。
     
     
    字符串可能有重复的,用int来记录数量
     
     
    //============================================================================
    // Name        : HDU.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <algorithm>
    #include <queue>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    struct Trie
    {
        int next[510][4],fail[510];
        int end[510];
        int root,L;
        int newnode()
        {
            for(int i = 0;i < 4;i++)
                next[L][i] = -1;
            end[L++] = 0;
            return L-1;
        }
        void init()
        {
            L = 0;
            root = newnode();
        }
        int getch(char ch)
        {
            if(ch == 'A')return 0;
            else if(ch == 'C')return 1;
            else if(ch == 'G')return 2;
            else return 3;
        }
        void insert(char buf[])
        {
            int len = strlen(buf);
            int now = root;
            for(int i = 0;i < len;i++)
            {
                if(next[now][getch(buf[i])] == -1)
                    next[now][getch(buf[i])] = newnode();
                now = next[now][getch(buf[i])];
            }
            end[now] ++;
        }
        void build()
        {
            queue<int>Q;
            fail[root] = root;
            for(int i = 0;i < 4;i++)
                if(next[root][i] == -1)
                    next[root][i] = root;
                else
                {
                    fail[next[root][i]] = root;
                    Q.push(next[root][i]);
                }
            while(!Q.empty())
            {
                int now = Q.front();
                Q.pop();
                end[now] += end[fail[now]];/********/
                for(int i = 0;i < 4;i++)
                    if(next[now][i] == -1)
                        next[now][i] = next[fail[now]][i];
                    else
                    {
                        fail[next[now][i]] = next[fail[now]][i];
                        Q.push(next[now][i]);
                    }
            }
        }
        int dp[510][11*11*11*11+5];
        int bit[4];
        int num[4];
        int solve(char buf[])
        {
            int len = strlen(buf);
            memset(num,0,sizeof(num));
            for(int i = 0;i < len;i++)
                num[getch(buf[i])]++;
            bit[0] = (num[1]+1)*(num[2]+1)*(num[3]+1);
            bit[1] = (num[2]+1)*(num[3]+1);
            bit[2] = (num[3]+1);
            bit[3] = 1;
            memset(dp,-1,sizeof(dp));
            dp[root][0] = 0;
            for(int A = 0;A <= num[0];A++)
                for(int B = 0;B <= num[1];B++)
                    for(int C = 0;C <= num[2];C++)
                        for(int D = 0;D <= num[3];D++)
                        {
                            int s = A*bit[0] + B*bit[1] + C*bit[2] + D*bit[3];
                            for(int i = 0;i < L;i++)
                                if(dp[i][s] >= 0)
                                {
                                    for(int k = 0;k < 4;k++)
                                    {
                                        if(k == 0 && A == num[0])continue;
                                        if(k == 1 && B == num[1])continue;
                                        if(k == 2 && C == num[2])continue;
                                        if(k == 3 && D == num[3])continue;
                                        dp[next[i][k]][s+bit[k]] = max(dp[next[i][k]][s+bit[k]],dp[i][s]+end[next[i][k]]);
                                    }
                                }
                        }
            int ans = 0;
            int status = num[0]*bit[0] + num[1]*bit[1] + num[2]*bit[2] + num[3]*bit[3];
            for(int i = 0;i < L;i++)
                ans = max(ans,dp[i][status]);
            return ans;
        }
    };
    char buf[50];
    Trie ac;
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int n;
        int iCase = 0;
        while( scanf("%d",&n) == 1 && n)
        {
            iCase++;
            ac.init();
            for(int i = 0;i < n;i++)
            {
                scanf("%s",buf);
                ac.insert(buf);
            }
            ac.build();
            scanf("%s",buf);
            printf("Case %d: %d
    ",iCase,ac.solve(buf));
        }
        return 0;
    }
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    #Git 21天打卡第一天 01天0526
    老徐第六期百人计划之职业发展方向&学习方向
    LR12.53安装中文补丁包,录制后回放脚本一致卡在编译的问题
    常用oracle语句整理
    LoadRunner11之批量插入SQL数据~2
    LoadRunner12之SQLServer数据库批量插入--.Net协议
    Jmeter连接Oracle数据库简单使用
    AppScan安装使用
    SQL多表连接
    [剑指Offer] 4.二维数组的查找
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3163648.html
Copyright © 2020-2023  润新知