Lost’s revenge

Time limit:5000ms Memory limit:65535kB

链接

http://acm.hdu.edu.cn/showproblem.php?pid=3341

题意

给出n个子串,对于给定的母串我们重新排列,问所有的排列中含有子串个数(相同的子串多次出现时重复计数)最多的有多少个?

思路

题解

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

typedef long long ll;

const int maxn = 505;
const int kind = 4;

int root, cnt;
int nxt[maxn][kind];
int fail[maxn];
int mrk[maxn];

int dic[128];

int newnode ()
{
    ++cnt;
    for (int i = 0; i < kind; ++i) {
        nxt[cnt][i] = -1;
    }
    mrk[cnt] = 0;
    return cnt;
}

void ac_init ()
{
    cnt = -1;
    root = newnode();
}

void wd_insert (char str[])
{
    int now = root;
    int len = strlen(str);
    for (int i = 0; i < len; ++i) {
        if (nxt[now][dic[str[i]]] == -1) {
            nxt[now][dic[str[i]]] = newnode();
        }
        now = nxt[now][dic[str[i]]];
    }
    ++mrk[now];
}

void build ()
{
    queue<int> q;
    fail[root] = root;
    for (int i = 0; i < kind; ++i) {
        if (nxt[root][i] == -1) {
            nxt[root][i] = root;
        }
        else {
            fail[nxt[root][i]] = root;
            q.push(nxt[root][i]);
        }
    }
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        mrk[now] += mrk[fail[now]];
        for (int i = 0; i < kind; ++i) {
            if (nxt[now][i] == -1) {
                nxt[now][i] = nxt[fail[now]][i];
            }
            else {
                fail[nxt[now][i]] = nxt[fail[now]][i];
                q.push(nxt[now][i]);
            }
        }
    }
}

void init ()
{
    dic['A'] = 0;
    dic['C'] = 1;
    dic['G'] = 2;
    dic['T'] = 3;
}

int n;

char str[45];
int st[4];

int dp[maxn][11*11*11*11+5];

int state (int t0, int t1, int t2, int t3)
{
    return t0*(st[1]+1)*(st[2]+1)*(st[3]+1)+t1*(st[2]+1)*(st[3]+1)+t2*(st[3]+1)+t3;
}

int tmp[4];

int main ()
{
    init();
    int cas = 1;
    while (scanf("%d", &n) != EOF && n) {
        ac_init();
        st[0] = st[1] = st[2] = st[3] = 0;
        for (int i = 1; i <= n; ++i) {
            scanf("%s", str);
            wd_insert(str);
        }
        build();
        scanf("%s", str);
        int len = strlen(str);
        for (int i = 0; i < len; ++i) {
            ++st[dic[str[i]]];
        }
        memset(dp, -1, sizeof(dp));
        dp[0][0] = 0;
        tmp[0] = (st[1]+1)*(st[2]+1)*(st[3]+1);
        tmp[1] = (st[2]+1)*(st[3]+1);
        tmp[2] = st[3]+1;
        tmp[3] = 1;
        for (int A = 0; A <= st[0]; ++A) {
            for (int B = 0; B <= st[1]; ++B) {
                for (int C = 0; C <= st[2]; ++C) {
                    for (int D = 0; D <= st[3]; ++D) {
                        int s = A*tmp[0]+B*tmp[1]+C*tmp[2]+D*tmp[3];
                        for (int i = 0; i <= cnt; ++i) {
                            if (dp[i][s] != -1) {
                                for (int k = 0; k < kind; ++k) {
                                    if (k == 0 && A == st[0]) {
                                        continue;
                                    }
                                    if (k == 1 && B == st[1]) {
                                        continue;
                                    }
                                    if (k == 2 && C == st[2]) {
                                        continue;
                                    }
                                    if (k == 3 && D == st[3]) {
                                        continue;
                                    }
                                    dp[nxt[i][k]][s+tmp[k]] = max(dp[nxt[i][k]][s+tmp[k]], dp[i][s]+mrk[nxt[i][k]]);
                                }
                            }
                        }
                    }
                }
            }
        }
        int res = -1, tr = state(st[0], st[1], st[2], st[3]);
        for (int i = 0; i <= cnt; ++i) {
            res = max(res, dp[i][tr]);
        }
        printf("Case %d: %d\n", cas++, res);
    }
    return 0;
}

发表评论