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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值