HDU 4057 AC自动机+状压dp

题意:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4057
要求构造一条长度为l的字符串,构造的字符串中若含有给出的子串,就可以加上该子串的权值,但是同一子串只能算一次权值,问构造的字符串最大权值多少。


思路:

看到n只有10,典型的AC自动机状压的思路,dp[x][y][S]保存构造到第x个字符,到达结点y,且含有子串的状态为S是否可能,直接转移最后统计各种S下的最大权值即可。


代码:

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;

struct ACauto {
    int next[1005][4], fail[1005], end[1005];
    int root, sz;

    int newnode() {
        for (int i = 0; i < 4; i++)
            next[sz][i] = -1;
        end[sz++] = 0;
        return sz - 1;
    }

    void init() {
        sz = 0;
        root = newnode();
    }

    int idx(char c) {
        if (c == 'A') return 0;
        if (c == 'C') return 1;
        if (c == 'G') return 2;
        return 3;
    }

    void insert(char *buf, int id) {
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; i++) {
            int id = idx(buf[i]);
            if (next[now][id] == -1)
                next[now][id] = newnode();
            now = next[now][id];
        }
        end[now] |= (1 << id);
    }

    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]);
                }
            }
        }
    }

} ac;

int n, len;
bool dp[2][1005][1100];
int val[1100], v[15];
const char d[] = {'A', 'C', 'G', 'T'};

void solve() {
    for (int S = 0; S < (1 << n); S++) {
        val[S] = 0;
        for (int i = 0; i < n; i++) {
            if (S & (1 << i)) val[S] += v[i];
        }
    }
    memset(dp, false, sizeof(dp));
    dp[0][0][0] = true;
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < ac.sz; j++)
            for (int S = 0; S < (1 << n); S++)
                dp[(i + 1) % 2][j][S] = false;
        for (int j = 0; j < ac.sz; j++) {
            for (int S = 0; S < (1 << n); S++) {
                if (!dp[i % 2][j][S]) continue;
                for (int k = 0; k < 4; k++) {
                    int ni = i + 1, nj = ac.next[j][k], nS = S | ac.end[nj];
                    dp[ni % 2][nj][nS] |= dp[i % 2][j][S];
                }
            }
        }
    }
    int ans = -INF;
    for (int j = 0; j < ac.sz; j++) {
        for (int S = 0; S < (1 << n); S++) {
            if (dp[len % 2][j][S])
                ans = max(ans, val[S]);
        }
    }
    if (ans < 0) puts("No Rabbit after 2012!");
    else printf("%d\n", ans);
}

char str[1005];

int main() {
    //freopen("in.txt", "r", stdin);
    while (scanf("%d%d", &n, &len) == 2) {
        ac.init();
        for (int i = 0; i < n; i++) {
            scanf("%s%d", str, &v[i]);
            ac.insert(str, i);
        }
        ac.build();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值