标题效果:

举个很多种DNA弦,每个字符串值值至1。最后,一个长字符串。要安排你最后一次另一个字符串,使其没事子值和最大。

IDEAS:

首先easy我们的想法是想搜索的!管她3721。。直接一个字符一个字符的码,然后在AC自己主动机上推断最后的权值。TLE哟。

然后发现搜过不去。那就dp咯。再easy想到的就是dp[i][a][b][c][d] 表示此时遍历AC自己主动机的节点在i,然后构成了a个A,b个G,c个C。d个T的权值。

再一看内存,500*40*40*40*40...然后。。。就没有然后了

再想,由于它说的是这个串的长度是40。所以最多的状态就是10*10*10*10.。,所以开40^4就是浪费。所以要把状态hash下来。

关于hash ...cnt[0]-cnt[3] 表示ACGT的数量。  如今表示ABCD个ACGT  就是A*(cnt[3]+1)*(cnt[2]+1)*(cnt[1]+1)+B*(cnt[2]+1)*(cnt[1]+1)+C*(cnt[1]+1)+D*1

然后就依照上面的五维dp去做就好啦。

坑点就是。

。又有反复的串,人与人之间最主要的信任都没有。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <utility>
#define inf 0x3f3f3f3f
#include <vector>
using namespace std; const char tab = 0;
const int max_next = 4;
int idx;
struct trie
{
struct trie *fail;
struct trie *next[max_next];
int isword;
int index;
};
int rev[256];
trie *que[100005],ac[100005];
int head,tail;
trie *New()
{
trie *temp=&ac[idx];
for(int i=0;i<max_next;i++)temp->next[i]=NULL;
temp->fail=NULL;
temp->isword=0;
temp->index=idx++;
return temp;
}
void Insert(trie *root,char *word,int len){
trie *t=root;
for(int i=0;i<len;i++){
if(t->next[rev[word[i]]]==NULL)
t->next[rev[word[i]]]=New();
t=t->next[rev[word[i]]];
}
t->isword++;
} void acbuild(trie *root){
int head=0,tail=0;
que[tail++]=root;
root->fail=NULL;
while(head<tail){
trie *temp=que[head++],*p;
for(int i=0;i<max_next;i++){
if(temp->next[i]){
if(temp==root)temp->next[i]->fail=root;
else {
p=temp->fail;
while(p!=NULL){
if(p->next[i]){
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)temp->next[i]->fail=root;
}
if(temp->next[i]->fail->isword)temp->next[i]->isword+=temp->next[i]->fail->isword;
que[tail++]=temp->next[i];
}
else if(temp==root)temp->next[i]=root;
else temp->next[i]=temp->fail->next[i];
}
}
}
void del()
{
for(int i=0;i<idx;i++)
free(&ac[i]);
}
void tra()
{
for(int i=0;i<idx;i++)
{
if(ac[i].fail!=NULL)printf("fail = %d ",ac[i].fail->index);
for(int k=0;k<max_next;k++)
printf("%d ",ac[i].next[k]->index);
puts("");
}
}
char word[50];
int cnt[5];
int dp[515][11*11*11*11+5];
int bit[5];
int solve()
{
memset(dp,-1,sizeof dp);
dp[0][0]=0;
bit[0]=(cnt[1]+1)*(cnt[2]+1)*(cnt[3]+1);
bit[1]=(cnt[2]+1)*(cnt[3]+1);
bit[2]=(cnt[3]+1);
bit[3]=1;
for(int A=0;A<=cnt[0];A++)
{
for(int B=0;B<=cnt[1];B++)
{
for(int C=0;C<=cnt[2];C++)
{
for(int D=0;D<=cnt[3];D++)
{
int state=A*bit[0]+B*bit[1]+C*bit[2]+D*bit[3];
for(int i=0;i<idx;i++)
{
if(dp[i][state]>=0)
{
for(int k=0;k<4;k++)
{
if(k==0 && A==cnt[0])continue;
if(k==1 && B==cnt[1])continue;
if(k==2 && C==cnt[2])continue;
if(k==3 && D==cnt[3])continue; dp[ac[i].next[k]->index][state+bit[k]]=max(dp[ac[i].next[k]->index][state+bit[k]],dp[i][state]+ac[i].next[k]->isword);
}
}
}
}
}
}
}
int ans=0;
int tag=cnt[0]*bit[0]+cnt[1]*bit[1]+cnt[2]*bit[2]+cnt[3]*bit[3];
for(int i=0;i<idx;i++)
ans=max(ans,dp[i][tag]);
return ans;
}
int main()
{
rev['A']=0;
rev['C']=1;
rev['G']=2;
rev['T']=3; int n,cas=1;
while(scanf("%d",&n)!=EOF && n)
{
idx=0;
trie *root = New();
for(int i=1;i<=n;i++)
{
scanf("%s",word);
Insert(root,word,strlen(word));
}
acbuild(root); scanf("%s",word);
int len=strlen(word);
memset(cnt,0,sizeof cnt);
for(int j=0;j<len;j++)
cnt[rev[word[j]]]++;
printf("Case %d: %d\n",cas++,solve());
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Hdu 3341 Lost&#39;s revenge (ac+自己主动机dp+hash)的更多相关文章

  1. HDU - 3341 Lost&#39;s revenge(AC自己主动机+DP)

    Description Lost and AekdyCoin are friends. They always play "number game"(A boring game b ...

  2. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  3. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  4. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  5. hdu4758 Walk Through Squares (AC自己主动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...

  6. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

  7. hdu4057 Rescue the Rabbit(AC自己主动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU - 4511 小明系列故事――女友的考验(AC自己主动机+DP)

    Description 最终放寒假了,小明要和女朋友一起去看电影.这天,女朋友想给小明一个考验,在小明正准备出发的时候.女朋友告诉他.她在电影院等他,小明过来的路线必须满足给定的规则:  1.如果小明 ...

  9. Hdu 2457 DNA repair (ac自己主动机+dp)

    题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...

随机推荐

  1. No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=arm64, VALID_ARCHS=armv7 armv7s)

    问题: No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=arm64, VALID_ARCHS=armv7 armv ...

  2. 分布式发布订阅消息系统Kafka

    高吞吐量的分布式发布订阅消息系统Kafka--安装及测试   一.Kafka概述 Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动作(网页浏览, ...

  3. Linux 利用hosts.deny 防止暴力破解ssh(转)

    一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...

  4. 找工作笔试面试那些事儿(8)---常问的CC++基础题

    这一部分是C/C++程序员在面试的时候会被问到的一些题目的汇总.来源于基本笔试面试书籍,可能有一部分题比较老,但是这也算是基础中的基础,就归纳归纳放上来了.大牛们看到一笑而过就好,普通人看看要是能补上 ...

  5. POj 1879 Tempus et mobilius Time and motion (模拟+群)

    题目特别长,大意为球的传递. 三个轨道,一个库.各自是分钟单位的轨道.5min单位的轨道.一小时单位的轨道.还有就是n容量的库. 每过一分钟,一个小球从库里面出来,库符合先进先出,进入分钟轨道.假设分 ...

  6. hdu1876(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1876 题意:问机器人到达终点的过程中最多有几次完全消耗完能量,消耗完这么多次能量的方式有几种. 分析: ...

  7. 【微信公众平台开发】公布动态新闻好帮手UEditor富文本

    因为微信要做公布动态新闻.那就须要富文本. 上网搜索有非常多这样的插件,比方CKEditor.KindEditor等:最后看到百度一款开源的UEditor.官网打开,风格设计就吸引住了自己.所以就选U ...

  8. MYSQL查询一周内的数据(最近7天的)、最近一个月、最近三个月数据

    如果你要严格要求是某一年的,那可以这样 查询一天: select * from table where to_days(column_time) = to_days(now()); select * ...

  9. As long as Binbin loves Sangsang

    题目连接 题意: 给定一个无向图,每一个边有两个属性.长度和一个字母'L','O','V'.'E'中的一个.从1点開始到达n点,每次必须依照L -> O -> V -> E -> ...

  10. 怎样在C++中获得完整的类型名称

    Wrote by mutouyun. (http://darkc.at/cxx-get-the-name-of-the-given-type/) 地球人都知道C++里有一个typeid操作符能够用来获 ...