hdu 3341 Lost's revenge
来源:互联网 发布:php制作动态网页作品 编辑:程序博客网 时间:2024/09/21 06:47
Lost's revenge
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4207 Accepted Submission(s): 1166
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.
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.
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
Author
Qinz@XDU
Source
HDOJ Monthly Contest – 2010.03.06
Recommend
lcy | We have carefully selected several similar problems for you: 3247 3336 2296 2825 2243
【分析】
AC自动机+dp
首先把所有小串建一个AC自动机
然后用dp[j][a][b][c][d]表示走到自动机的节点 j,而且现在用了文本串中的a个A,b个G,c个C,d个T所能匹配到的最多的小串数目。
发现空间炸了...
观察到文本串长度<=40,那么最多的组合应该是有10*10*10*10=1W种方式组合,那么我们把a,b,c,d不要开四维空间了,hash一下变成一维来防止MLE。
那么方程很好写了,具体请看代码实现吧。
还有就是要注意循环顺序,节点应该在最后面的循环。(WA哭了QAQ)
【代码】
//hdu 3341 Lost's revenge#include<iostream>#include<cstring>#include<cstdio>#include<queue>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=505;char s[mxn];queue <int> q;int n,m,T,num;int has[41][41][41][41];int map[150],tot[5],dp[mxn][11*11*11*11+5];struct node {int son[5],fail,cnt;} a[mxn];inline void clear(int x){ a[x].fail=a[x].cnt=0; memset(a[x].son,0,sizeof a[x].son);}inline void trie(){ scanf("%s",s+1); int i,j,x=0,len=strlen(s+1); fo(i,1,len) { int c=map[s[i]]; if(!a[x].son[c]) clear(++num),a[x].son[c]=num; x=a[x].son[c]; } a[x].cnt++;}inline void build(){ int i,j; fo(i,1,4) if(a[0].son[i]) q.push(a[0].son[i]); while(!q.empty()) { int x=q.front();q.pop(); int fail=a[x].fail; fo(i,1,4) { int y=a[x].son[i]; if(y) a[y].fail=a[fail].son[i],q.push(y); else a[x].son[i]=a[fail].son[i]; } a[x].cnt+=a[fail].cnt; }}inline void init(){ int A,B,C,D,czy=-1; fo(A,0,tot[1]) fo(B,0,tot[2]) fo(C,0,tot[3]) fo(D,0,tot[4]) has[A][B][C][D]=++czy;}inline int dynamic(){ init(); int i,j,k,A=0,B=0,C=0,D=0,ans=0; memset(dp,-0x3f,sizeof dp); dp[0][0]=0; fo(A,0,tot[1]) fo(B,0,tot[2]) fo(C,0,tot[3]) fo(D,0,tot[4]) fo(k,1,4) fo(j,0,num) { int S=has[A][B][C][D],s1,s2,s3,s4,tmp=a[a[j].son[k]].cnt; if(D && k==4) s1=has[A][B][C][D-1],dp[a[j].son[k]][S]=max(dp[a[j].son[k]][S],dp[j][s1]+tmp); if(C && k==3) s2=has[A][B][C-1][D],dp[a[j].son[k]][S]=max(dp[a[j].son[k]][S],dp[j][s2]+tmp); if(B && k==2) s3=has[A][B-1][C][D],dp[a[j].son[k]][S]=max(dp[a[j].son[k]][S],dp[j][s3]+tmp); if(A && k==1) s4=has[A-1][B][C][D],dp[a[j].son[k]][S]=max(dp[a[j].son[k]][S],dp[j][s4]+tmp);// if(dp[a[j].son[k]][S]>=0)// printf("树上节点%d 儿子 %d=%d %d %d %d=%d,new=%d\n",j,a[j].son[k],A,B,C,D,dp[a[j].son[k]][S],k); } int end=has[tot[1]][tot[2]][tot[3]][tot[4]]; fo(j,0,num) ans=max(ans,dp[j][end]); return ans;}int main(){ int i,j; map['A']=1,map['G']=2,map['C']=3,map['T']=4; while(scanf("%d",&n) && n) { M(tot); T++,num=0,clear(0); fo(i,1,n) trie(); build(); scanf("%s",s+1); int len=strlen(s+1); fo(i,1,len) tot[map[s[i]]]++; printf("Case %d: %d\n",T,dynamic()); } return 0;}/*3GTGGAGAGGT*/
阅读全文
0 0
- hdu 3341 Lost's revenge
- hdu 3341 Lost's revenge
- HDU 3341 Lost's revenge
- HDU 3341Lost's revenge
- hdu 3341 Lost's revenge
- hdu 3341 Lost's revenge(AC自动机+DP)
- HDU 3341 Lost's revenge(AC自动机+DP)
- 【HDU】3341 Lost's revenge AC自动机+变进制+DP
- Hdu 3341 Lost's revenge (ac自动机+dp+hash)
- HDU - 3341 Lost's revenge(AC自动机+DP)
- [AC自动机+dp+变进制状压] hdu 3341 Lost's revenge
- hdu 3341 Lost's revenge(AC自动机+变进制状压DP)
- HDU 3341 Lost's revenge AC自动机 + 变进制状压DP
- hdu 3341 Lost's revenge (ac自动机+状压dp)
- hdu 3341Lost's revenge(ac自动机+dp)
- hdu 3341 Lost's revenge(dp+Ac自动机)
- HDU 3341 Lost's revenge (AC自动机 + DP)
- HDU 3341-Lost's revenge(AC自动机+DP+hash)
- 4883: [Lydsy2017年5月月赛]棋盘上的守卫
- 在网页中使用个性字体
- 汇编--学习笔记(四)-DOS功能调用
- VS2010快速调整格式
- <测试架构师修炼之道>读书笔记1
- hdu 3341 Lost's revenge
- 将输入的密码变成*的代码
- 输出正整数二进制的位置(难度:半颗星)
- TCP组包问题
- C语言-数据结构-创建栈
- 基于jsp,servlet的简单图书馆管理软件
- 使用Java方式ftp下载
- CSUOJ-1044: 扑克排序
- redis ssm遇到的小问题2