Lost's revenge
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4317 Accepted Submission(s): 1199
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
3 AC CG GT CGAT 1 AA AAA 0
Sample Output
Case 1: 3 Case 2: 2
题意:给你n个模式串,然后给你一个目标串,重组这个目标串,使得含有的模式串数量最多,输出这个最大值。
题解:首先肯定是要DP,具体怎么DP,一开始想的是5维DP,想想就行了,因此目标串的元素较多,超内存是肯定的,呢该怎么搞,不要忘了hash这个好东西Orz,定义hash[a][b][c][d]:表示A用了a个,T用了b个,G用了c个,C用了d个的状态的编号,然后定义DP[i][j]:表示自动机匹配到i结点且状态为j的最大匹配数量。。
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 1000000000
#define mod 100000
#define maxn 550
#define lowbit(x) (x&-x)
#define eps 1e-10
int pre[maxn],a[maxn][5],flag[maxn],size,n,m,cases;
int num[5],dp[maxn][15000],hashs[45][45][45][45];
char s1[25],s2[55];
queue<int>q;
struct node
{
ll mat[105][105];
}b,d;
int c(char x)
{
if(x=='A')return 0;
if(x=='T')return 1;
if(x=='G')return 2;
if(x=='C')return 3;
}
void insert(int num)
{
int i,len=strlen(s1),now=0,cnt=0;
for(i=0;i<len;i++)
{
int v=c(s1[i]);
if(!a[now][v])
{
flag[size]=0;
memset(a[size],0,sizeof(a[size]));
a[now][v]=size++;
}
now=a[now][v];
}
flag[now]++;
}
void build_fail()
{
int now,i;
for(i=0;i<4;i++)
{
int tmp=a[0][i];
if(tmp)
pre[tmp]=0,q.push(tmp);
}
while(q.empty()==0)
{
now=q.front();
q.pop();
if(flag[pre[now]])
flag[now]+=flag[pre[now]];
for(i=0;i<4;i++)
{
if(a[now][i]==0)
{
a[now][i]=a[pre[now]][i];
continue;
}
pre[a[now][i]]=a[pre[now]][i];
q.push(a[now][i]);
}
}
}
void work()
{
int i,j,k,h,t1,t2,len=strlen(s2),cnt=0,ans=0;
for(i=0;i<len;i++)
num[c(s2[i])]++;
for(i=0;i<=num[0];i++)
for(j=0;j<=num[1];j++)
for(k=0;k<=num[2];k++)
for(h=0;h<=num[3];h++)
hashs[i][j][k][h]=cnt++;
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(i=0;i<=num[0];i++)
for(j=0;j<=num[1];j++)
for(k=0;k<=num[2];k++)
for(h=0;h<=num[3];h++)
{
int tmp=hashs[i][j][k][h];
if(i+j+k+h==0)
continue;
for(t1=0;t1<size;t1++)
{
for(t2=0;t2<4;t2++)
{
int tmp1;
if(t2==0 && i>=1)
tmp1=hashs[i-1][j][k][h];
else if(t2==1 && j>=1)
tmp1=hashs[i][j-1][k][h];
else if(t2==2 && k>=1)
tmp1=hashs[i][j][k-1][h];
else if(t2==3 && h>=1)
tmp1=hashs[i][j][k][h-1];
else
continue;
if(dp[t1][tmp1]==-1)
continue;
dp[a[t1][t2]][tmp]=max(dp[a[t1][t2]][tmp],dp[t1][tmp1]+flag[a[t1][t2]]);
ans=max(ans,dp[a[t1][t2]][tmp]);
}
}
}
printf("Case %d: %d\n",++cases,ans);
}
int main(void)
{
int i;
while(scanf("%d",&n),n!=0)
{
size=1;pre[0]=0;flag[0]=0;
memset(a[0],0,sizeof(a[0]));
memset(pre,0,sizeof(pre));
memset(num,0,sizeof(num));
for(i=1;i<=n;i++)
{
scanf("%s",s1);
insert(i);
}
build_fail();
scanf("%s",s2);
work();
}
return 0;
}