HDU3341 Lost's revenge

Lost's revenge

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



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
 
题意:给出几个数论基因,要求将一个字符串重新排列使这几个数论基因最多(可以重叠),输出数论基因出现最多的次数。
分析:AC自动机+状态压缩DP,必须要进行状态压缩,不然就要开一个5维数组,表示A,C,G,T的个数和走到树上一节点的最大个数,压缩之后dp[i][j],i就表示一个状态,j表示走到了哪个节点。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXM=15000;
const int MAXN=510;
int size;
int dp[MAXM][MAXN],K[5];
char s[MAXN];
bool vis[MAXM];
struct node
{
    int fail;
    int next[4];
    int cnt;
    void init()
    {
        fail=cnt=0;
        memset(next,0,sizeof(next));
    }
}tree[MAXN];
int get(char c)
{
    if(c=='A')
        return 0;
    if(c=='C')
        return 1;
    if(c=='G')
        return 2;
    return 3;
}
void insert(char *str)
{
    int i=0,p=0,index;
    while(str[i])
    {
        index=get(str[i]);
        if(tree[p].next[index]==0)
        {
            tree[++size].init();
            tree[p].next[index]=size;
        }
        p=tree[p].next[index];
        i++;
    }
    tree[p].cnt++;
}
void build_ac_automation()
{
    int i;
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            if(tree[temp].next[i])
            {
                int p=tree[temp].next[i];
                if(temp)
                {
                    tree[p].fail=tree[tree[temp].fail].next[i];
                    tree[p].cnt+=tree[tree[p].fail].cnt;
                }
                q.push(p);
            }
            else
                tree[temp].next[i]=tree[tree[temp].fail].next[i];
        }
    }
}
int flag=1;
void solve()
{
    queue<int> q;
    int head,temp;
    int i,j,k,ans,cnt[4];
    memset(dp,-1,sizeof(dp));
    memset(cnt,0,sizeof(cnt));
    memset(vis,0,sizeof(vis));
    for(i=0;s[i];i++)
    {
        cnt[get(s[i])]++;
    }
    for(i=0;i<5;i++)
    {
        if(i)
            K[i]=K[i-1]*(cnt[i-1]+1);
        else
            K[i]=1;
    }
    ans=dp[0][0]=0;
    q.push(0);
    vis[0]=1;
    while(!q.empty())
    {
        head=q.front();
        q.pop();
        for(i=0;i<=size;i++)
        {
            if(dp[head][i]<0)
                continue;
            for(j=0;j<4;j++)
            {
                k=head%K[j+1]/K[j];
                if(k>=cnt[j])
                    continue;
                temp=head+K[j]; //j(对应一个碱基)的状态
                k=tree[i].next[j];  //从i节点到j节点
                if(dp[temp][k]<dp[head][i]+tree[k].cnt)
                {
                    dp[temp][k]=dp[head][i]+tree[k].cnt;
                    ans=max(ans,dp[temp][k]);
                    if(!vis[temp])
                    {
                        vis[temp]=1;
                        q.push(temp);
                    }
                }
            }
        }
    }
    printf("Case %d: %d\n",flag++,ans);
}
int main()
{
    int n,i;
    while(scanf("%d",&n)==1&&n)
    {
        size=0;
        tree[0].init();
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            insert(s);
        }
        scanf("%s",s);
        build_ac_automation();
        solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值