HDU3341 Lost's revenge
2014-07-06 12:27
351 查看
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
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each testcase, output the case number(start with 1) and the most "level of number theory" with format like the sample output.
[align=left]Sample Input[/align]
3 AC CG GT CGAT 1 AA AAA 0
[align=left]Sample Output[/align]
Case 1: 3 Case 2: 2
[align=left]Author[/align]
Qinz@XDU
[align=left]Source[/align]
HDOJ Monthly Contest – 2010.03.06
题意:给出几个数论基因,要求将一个字符串重新排列使这几个数论基因最多(可以重叠),输出数论基因出现最多的次数。
分析: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; }
相关文章推荐
- HDU 3341 Lost's revenge(AC自动机+DP)
- Hdu 3341 Lost's revenge (ac自动机+dp+hash)
- hdu 3341 Lost's revenge
- hdu 3341 Lost's revenge(AC自动机+DP)
- hdu 3341 Lost's revenge(dp+Ac自动机)
- Hdu 3341 Lost's revenge (ac+自己主动机dp+hash)
- HDU - 3341 Lost's revenge(AC自动机+DP)
- HDU - 3341 Lost's revenge(AC自己主动机+DP)
- [AC自动机+dp+变进制状压] hdu 3341 Lost's revenge
- HDU 3341 Lost's revenge
- HDU 3341 Lost's revenge(AC自动机+状态压缩DP)
- 【AC自动机】 HDOJ 3341 Lost's revenge
- ZOJ_3569_Dr. Zomboss's Revenge(概率期望)
- UVa816 Abbott's Revenge
- 手把手解决Mysql错误:Lost connection to MySQL server at 'reading authorization packet', system error: 0
- uva 816 - Abbott's Revenge(有一点难度的bfs迷宫题目)
- mysql连接远程数据库出现 ERROR 2013 (HY000) Lost connection to MySQL server at 'reading authorization packet',
- 816 - Abbott's Revenge(BFS)
- 有关Mysql出现Lost connection to MySQL server at 'reading initial communication packet', system error: 0的一个解决方案