HDU3341 AC自動機上的狀壓DP

傳送門:點擊打開鏈接

Lost's revenge

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


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

題意,給出n個基因的字符串(只含有A C G T字符),每個串的長度的 長度不超過10。再給一個串S,長度不超過40.然你重新排列S的順序後,使上述的基因串在新的S串中出現的次數最多。PS:要記交叉和重複的。
思路:先將給出的n個字符串構建成AC自動機。考慮到給出的S串的長度只有40,意味着在S串中,A+C+G+T是不超過40的。那麼使用ACGT的已使用數量的組合的情況就不會超過15000(這個都有點大了)。在這這裏,我們對ACGT的每一個鹼基的使用量進行狀壓,如下:
int A,C,G,T;
int  exsys(int a,int c,int g,int t)
{
    int BT=1;
    int BG=BT*(T+1);
    int BC=BG*(G+1);
    int BA=BC*(C+1);
    return a*BA+c*BC+g*BG+t;
}
用dp[I][j]表示在鹼基使用情況爲I的時候,在AC自動機j點的時候,獲得的最大答案。每一轉移,我們枚舉下個鹼基是A、C、G、T,轉移出不同的新的狀態。
由於時間卡的太緊。交G++果斷TLE。但是交C++就過了
#include<cstdio>
#include<cstring>
#define SIGMA_SIZE 4
#define maxn 520
#include<queue>
using namespace std;
int ch[maxn][SIGMA_SIZE];
int val[maxn];
int last[maxn], f[maxn];
int cnt;
int A,C,G,T;
int  exsys(int a,int c,int g,int t)
{
    int BT=1;
    int BG=BT*(T+1);
    int BC=BG*(G+1);
    int BA=BC*(C+1);
    return a*BA+c*BC+g*BG+t;
}
inline int idx(char c)
{
    if(c=='A') return 0;
    else if(c=='C') return 1;
    else if(c=='G') return 2;
    else return 3;
}
void insert(char s[])
{
    int len = strlen(s);
    int u = 0;
    for (int i = 0; i<len; i++)
    {
        int v = idx(s[i]);
        if (!ch[u][v]) ch[u][v] = ++cnt;
        u = ch[u][v];
    }
    val[u]++;
}
int dp[15000][505];
void slove(char s[])
{
    int ANS=0;
    int len=strlen(s);
    A=0;C=0;G=0;T=0;
    for(int i=0;i<len;i++)
    if(s[i]=='A') A++;
    else if(s[i]=='C') C++;
    else if(s[i]=='G') G++;
    else T++;
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    for(int a=0;a<=A;a++)
    for(int c=0;c<=C;c++)
    for(int g=0;g<=G;g++)
    for(int t=0;t<=T;t++)
    for(int p=0;p<=cnt;p++)
    {
        int now=exsys(a,c,g,t);
        if(dp[now][p]==-1) continue;
        if(dp[now][p]>ANS) ANS=dp[now][p];
        if(a<A)
        {
            int j=p;
            int to=exsys(a+1,c,g,t);
            int cc=idx('A');
            while(j&&!ch[j][cc]) j=f[j];
            j = ch[j][cc];
            int ans=0;
            if (val[j]) ans+=val[j];
            int tmp = last[j];
            while (tmp)
            {
                ans+=val[tmp];
                tmp = last[tmp];
            }
            if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
        }
        if(t<T)
        {
            int j=p;
            int to=exsys(a,c,g,t+1);
            int cc=idx('T');
            while(j&&!ch[j][cc]) j=f[j];
            j = ch[j][cc];
            int ans=0;
            if (val[j]) ans+=val[j];
            int tmp = last[j];
            while (tmp)
            {
                ans+=val[tmp];
                tmp = last[tmp];
            }
            if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
        }
        if(c<C)
        {
            int j=p;
            int to=exsys(a,c+1,g,t);
            int cc=idx('C');
            while(j&&!ch[j][cc]) j=f[j];
            j = ch[j][cc];
            int ans=0;
            if (val[j]) ans+=val[j];
            int tmp = last[j];
            while (tmp)
            {
                ans+=val[tmp];
                tmp = last[tmp];
            }
            if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
        }
        if(g<G)
        {
            int j=p;
            int to=exsys(a,c,g+1,t);
            int cc=idx('G');
            while(j&&!ch[j][cc]) j=f[j];
            j = ch[j][cc];
            int ans=0;
            if (val[j]) ans+=val[j];
            int tmp = last[j];
            while (tmp)
            {
                ans+=val[tmp];
                tmp = last[tmp];
            }
            if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
        }
    }
    printf("%d\n",ANS);
}
void getFail()
{
    queue<int>q;
    f[0] = 0;
    for (int c = 0; c<SIGMA_SIZE; c++)
    {
        int u = ch[0][c];
        if (u)
        {
            f[u] = 0;
            q.push(u);
            last[u] = 0;
        }
    }
    while (!q.empty())
    {
        int r = q.front();
        q.pop();
        for (int c = 0; c<SIGMA_SIZE; c++)
        {
            int u = ch[r][c];
            if (!u)
            {
                continue;
            }
            q.push(u);
            int v = f[r];
            while (v&&!ch[v][c]) v = f[v];
            f[u] = ch[v][c];
            last[u] = val[f[u]] ? f[u] : last[f[u]];
        }
    }
}
int main()
{
    int n;
    int ks=0;
    while (scanf("%d", &n),n)
    {
        memset(ch, 0, sizeof(ch));
        memset(val, 0, sizeof(val));
        cnt = 0;
        char s[100];
        for (int i = 1; i <= n; i++)
        {
            scanf("%s", s);
            insert(s);
        }
        getFail();
        scanf("%s", s);
        ks++;
        printf("Case %d: ",ks);
        slove(s);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章