HDU 3341 Lost's revenge

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

ac自动机+多进制状压dp

#include<stdio.h>
#include<string.h>
#include<queue>
#include<malloc.h>
using namespace std;
const int maxn = 505;
int n;

class tire
{
public:
    tire *down[26], *next;
    int end, id;
    tire(){ next = NULL; id = end = 0; memset(down, 0, sizeof(down)); }
};

class ac_automaton
{
private:
    tire *root;
    tire *node[maxn];
    int tot, pos[4], sum;
    int ch[4];
    char s[maxn];
    int f[maxn][maxn * 30];
public:
    ac_automaton(){ node[0] = root = new tire; tot = 0; }
    void clear(){ node[0] = root = new tire; tot = 0; }

    void insert()
    {
        scanf("%s", s);
        tire* j = root;
        for (int i = 0, k; s[i]; i++)
        {
            k = s[i] - 'A';
            if (!j->down[k])
            {
                node[++tot] = j->down[k] = new tire;
                node[tot]->id = tot;
            }
            j = j->down[k];
        }
        j->end++;
    }

    void getnext()
    {
        queue<tire*> p;
        tire *q, *k, *j;

        root->next = root;
        for (int i = 0; i < 26; i++)
        if (root->down[i])
        {
            q = root->down[i];
            q->next = root;
            p.push(q);
        }
        else root->down[i] = root;

        while (!p.empty())
        {
            q = p.front();    p.pop();
            k = q->next;
            q->end += k->end;

            for (int i = 0; i < 26; i++)
            if (q->down[i])
            {
                j = q->down[i];
                j->next = k->down[i];
                p.push(q->down[i]);
            }
            else q->down[i] = k->down[i];
        }
    }

    void getpos()
    {
        scanf("%s", s);
        ch[0] = 'A' - 'A';
        ch[1] = 'C' - 'A';
        ch[2] = 'G' - 'A';
        ch[3] = 'T' - 'A';
        memset(pos, 0, sizeof(pos));
        for (int i = 0; s[i]; i++)
        {
            if (s[i] == 'A') pos[0]++;
            if (s[i] == 'C') pos[1]++;
            if (s[i] == 'G') pos[2]++;
            if (s[i] == 'T') pos[3]++;
        }
        sum = (pos[0] + 1) * (pos[1] + 1) * (pos[2] + 1) * (pos[3] + 1) - 1;
        pos[0] = (pos[1] + 1) * (pos[2] + 1) * (pos[3] + 1);
        pos[1] = (pos[2] + 1) * (pos[3] + 1);
        pos[2] = (pos[3] + 1);
        pos[3] = 1;
    }

    int work_out()
    {
        getnext();
        getpos();
        int ans = 0;
        memset(f, -1, sizeof(f));
        f[0][sum] = 0;
        for (int i = sum; i >= 0; i--)
        {
            for (int j = 0; j <= tot; j++)
            if (f[j][i] >= 0)
            {
                int I = i;
                for (int k = 0; k < 4; k++)
                {
                    if (I / pos[k])
                    {
                        int &u = f[node[j]->down[ch[k]]->id][i - pos[k]];
                        u = max(u, f[j][i] + node[j]->down[ch[k]]->end);
                    }
                    I %= pos[k];
                }
                ans = max(ans, f[j][i]);
            }
        }
        return ans;
    }
}F;

int main()
{
    int z = 0;
    while (scanf("%d", &n), n)
    {
        F.clear();
        for (int i = 0; i < n; i++) F.insert();
        printf("Case %d: %d\n", ++z, F.work_out());
    }
    return 0;
}


你可能感兴趣的:(HDU,AC自动机)