AC自动机+hdu3341

来源:互联网 发布:排水系统模拟软件 编辑:程序博客网 时间:2024/05/15 11:46

Online JudgeOnline ExerciseOnline TeachingOnline ContestsExercise AuthorF.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts

Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author lee
Mail Mail 0(0)
Control Panel Control Panel 
Sign Out Sign Out
BestCoder官方QQ群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

Lost's revenge

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


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
3ACCGGTCGAT1AAAAA0
 

Sample Output
Case 1: 3Case 2: 2


题意:给出n个病毒串,然后给出一个字符串,重新排序后最多包含多少个病毒串

思路:统计串中A,C,G,T的个数,然后用他们去重新构造这个串,构造的时候用类似进制转化的方式构造状态,不然会超时

dp[i][j]表示i状态包含a个A,b个C,c个G,d个T,并且处在AC自动机的节点j的时候最多包涵多少个

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=510;const int SIGMA_SIZE=4;int N;char s[20],str[50];int dp[maxn][11*11*11*11],num[5],bit[5];struct AC{    int ch[maxn][26],val[maxn];    int fail[maxn],last[maxn];    int sz;    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}    int idx(char x)    {        if(x=='A')return 0;        else if(x=='C')return 1;        else if(x=='G')return 2;        return 3;    }    void insert(char *s)    {        int n=strlen(s);        int u=0;        for(int i=0;i<n;i++)        {            int c=idx(s[i]);            if(!ch[u][c])            {                memset(ch[sz],0,sizeof(ch[sz]));                val[sz]=0;                ch[u][c]=sz++;            }            u=ch[u][c];        }        val[u]++;    }    void getfail()    {        queue<int> q;        int u=0;        fail[0]=0;        for(int i=0;i<SIGMA_SIZE;i++)        {            u=ch[0][i];            if(u){fail[u]=last[u]=0;q.push(u);}        }        while(!q.empty())        {            int r=q.front();q.pop();            val[r]+=val[fail[r]];            for(int c=0;c<SIGMA_SIZE;c++)            {                u=ch[r][c];                if(!u){ch[r][c]=ch[fail[r]][c];continue;}                q.push(u);                int v=fail[r];                while(v&&!ch[v][c])v=fail[v];                fail[u]=ch[v][c];                last[u]=val[fail[u]]?fail[u]:last[fail[u]];            }        }    }    int solve()    {        int len=strlen(str);        //cout<<len<<endl;        memset(num,0,sizeof(num));        for(int i=0;i<len;i++)num[idx(str[i])]++;        memset(dp,-1,sizeof(dp));        dp[0][0]=0;        bit[0]=(num[1]+1)*(num[2]+1)*(num[3]+1);        bit[1]=(num[2]+1)*(num[3]+1);        bit[2]=(num[3]+1);        bit[3]=1;        for(int A=0;A<=num[0];A++)            for(int B=0;B<=num[1];B++)                for(int C=0;C<=num[2];C++)                    for(int D=0;D<=num[3];D++)                    {                        int s=A*bit[0]+B*bit[1]+C*bit[2]+D*bit[3];                        for(int i=0;i<sz;i++)                        {                            if(dp[i][s]<0)continue;                            for(int k=0;k<4;k++)                            {                                if(k==0&&A==num[0])continue;                                if(k==1&&B==num[1])continue;                                if(k==2&&C==num[2])continue;                                if(k==3&&D==num[3])continue;                                dp[ch[i][k]][s+bit[k]]=max(dp[ch[i][k]][s+bit[k]],dp[i][s]+val[ch[i][k]]);                                //cout<<dp[ch[i][k]][s+bit[k]]<<endl;                            }                        }                    }        int ans=0;        int s=num[0]*bit[0]+num[1]*bit[1]+num[2]*bit[2]+num[3]*bit[3];        for(int i=0;i<sz;i++)            ans=max(ans,dp[i][s]);        return ans;    }}ac;int main(){    int cas=1;    while(scanf("%d",&N)!=EOF,N)    {        ac.clear();        for(int i=1;i<=N;i++)        {            scanf("%s",s);//cout<<s<<endl;            ac.insert(s);        }        ac.getfail();        scanf("%s",str);//cout<<str<<endl;        printf("Case %d: %d\n",cas++,ac.solve());    }    return 0;}




0 0
原创粉丝点击