HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )

模板来自notonlysuccess.

模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩。

将模式串加入AC自动机,最多有10*100个状态。

dp[i][j][k]:串长为i,在Trie图上的状态为j,已经包含的模式串为k(二进制表示,第x位为1代表已经包含第x个串)。

dp[i][j][k]为true或false代表该状态是否可达。

沿着Trie图中的边走进行DP,时间复杂度O( 100*1000*1024 );

最后枚举一下串长为L的所有可达状态,最大值即为结果。

PS.内存有限,需要用滚动数组

PS2.若使用静态队列的话,队列大小最好开状态数的二倍或者更多

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAX_NODE = 1010;
const int CHILD_NUM = 4;
const int MAXN = 12;
const int INF = 1 << 30;

struct ACAutomaton
{
    int chd[MAX_NODE][CHILD_NUM]; //每个节点的儿子,即当前节点的状态转移
    int val[MAX_NODE];            //记录题目给的关键数据
    int fail[MAX_NODE];           //传说中的fail指针
    int Q[MAX_NODE<<1];           //队列,用于广度优先计算fail指针
    int ID[128];                  //字母对应的ID
    int sz;                       //已使用节点个数

    //初始化,计算字母对应的儿子ID,如:'a'->0 ... 'z'->25
    void Initialize()
    {
        fail[0] = 0;
        ID['A'] = 0;
        ID['G'] = 1;
        ID['C'] = 2;
        ID['T'] = 3;
        return;
    }
    //重新建树需先Reset
    void Reset()
    {
        memset(chd[0] , 0 , sizeof(chd[0]));
        val[0] = 0;
        sz = 1;
    }
    //将权值为key的字符串a插入到trie中
    void Insert(char *a,int key)
    {
        int p = 0;
        for ( ; *a ; a ++)
        {
            int c = ID[*a];
            if (!chd[p][c])
            {
                memset(chd[sz] , 0 , sizeof(chd[sz]));
                val[sz] = 0;
                chd[p][c] = sz ++;
            }
            p = chd[p][c];
        }
        val[p] = key;
    }
    //建立AC自动机,确定每个节点的权值以及状态转移
    void Construct()
    {
        int *s = Q , *e = Q;
        for (int i = 0 ; i < CHILD_NUM ; i ++)
        {
            if (chd[0][i])
            {
                fail[ chd[0][i] ] = 0;
                *e ++ = chd[0][i];
            }
        }
        while (s != e)
        {
            int u = *s++;
            for (int i = 0 ; i < CHILD_NUM ; i ++)
            {
                int &v = chd[u][i];
                if (v)
                {
                    *e ++ = v;
                    fail[v] = chd[ fail[u] ][i];
                    //以下一行代码要根据题目所给val的含义来写
                    val[v] |= val[ fail[v] ];
                }
                else
                {
                    v = chd[ fail[u] ][i];
                }
            }
        }
    }
} AC;

int N, L;
int cost[MAXN];
bool dp[2][MAX_NODE][ (1 << 10) + 4 ];

int getVal( int S )
{
    int res = 0;
    for ( int j = 0; j < N; ++j )
        if ( S & ( 1 << j ) )
            res += cost[j];
    return res;
}

int main()
{
    char gene[210];
    AC.Initialize();
    while ( scanf( "%d%d", &N, &L ) == 2 )
    {
        AC.Reset();
        for ( int i = 0; i < N; ++i )
        {
            scanf( "%s%d", gene, &cost[i] );
            AC.Insert( gene, 1 << i );
        }
        AC.Construct();

        int all = 1 << N;

        memset( dp[0], false, sizeof(dp[0]) );
        int pre = 0, cur = 1;
        dp[0][0][0] = true;
        for ( int i = 1; i <= L; ++i )
        {
            memset( dp[cur], 0, sizeof(dp[cur]) );
            for ( int j = 0; j < AC.sz; ++j )
            {
                for ( int k = 0; k < 4; ++k )
                {
                    int next = AC.chd[j][k];
                    for ( int S = 0; S < all; ++S )
                    {
                        if ( dp[pre][j][S] )
                            dp[cur][next][ S|AC.val[next] ] = true;
                    }
                }
            }
            pre ^= 1;
            cur ^= 1;
        }

        int ans = -1;
        for ( int i = 0; i < all; ++i )
        {
            for ( int j = 0; j < AC.sz; ++j )
            {
                if ( dp[pre][j][i] )
                {
                    ans = max( ans, getVal(i) );
                    break;
                }
            }
        }

        if ( ans < 0 ) puts("No Rabbit after 2012!");
        else printf( "%d\n", ans );
    }
    return 0;
}

 

posted @ 2013-09-19 17:07  冰鸮  阅读(251)  评论(0编辑  收藏  举报