您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: