http://acm.hdu.edu.cn/showproblem.php?pid=4057

Problem Description
Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, and Dr. X wants to take some rabbits to Noah's Ark, or there are no rabbits any more.



A rabbit's genes can be expressed as a string whose length is l (1 ≤ l ≤ 100) containing only 'A', 'G', 'T', 'C'. There is no doubt that Dr. X had a in-depth research on the rabbits' genes. He found that if a rabbit gene contained a particular gene segment,
we could consider it as a good rabbit, or sometimes a bad rabbit. And we use a value W to measure this index.



We can make a example, if a rabbit has gene segment "ATG", its W would plus 4; and if has gene segment "TGC", its W plus -3. So if a rabbit's gene string is "ATGC", its W is 1 due to ATGC contains both "ATG"(+4) and "TGC"(-3). And if another rabbit's gene string
is "ATGATG", its W is 4 due to one gene segment can be calculate only once.



Because there are enough rabbits on Earth before 2012, so we can assume we can get any genes with different structure. Now Dr. X want to find a rabbit whose gene has highest W value. There are so many different genes with length l, and Dr. X is not good at
programming, can you help him to figure out the W value of the best rabbit.
 
Input
There are multiple test cases. For each case the first line is two integers n (1 ≤ n ≤ 10),l (1 ≤ l ≤ 100), indicating the number of the particular gene segment and the length of rabbits' genes.



The next n lines each line contains a string DNAi and an integer wi (|wi| ≤ 100), indicating this gene segment and the value it can contribute to a rabbit's W.
 
Output
For each test case, output an integer indicating the W value of the best rabbit. If we found this value is negative, you should output "No Rabbit after 2012!".
 
Sample Input
2 4
ATG 4
TGC -3 1 6
TGC 4 4 1
A -1
T -2
G -3
C -4
 
Sample Output
4
4
No Rabbit after 2012!
Hint
case 1:we can find a rabbit whose gene string is ATGG(4), or ATGA(4) etc.
case 2:we can find a rabbit whose gene string is TGCTGC(4), or TGCCCC(4) etc.
case 3:any gene string whose length is 1 has a negative W.
/**
hdu 4057 AC自己主动机+状态压缩dp
题目大意:给定一些字符串(ATGC组成)。相同用该四个字符组成一个长度为l的字符串,若该字符串包括给定串的一个作为子串,那么就要
加上这个给定串的值,问最后该字符串的最大值为多少
解题思路:建立自己主动机,然后在上面跑,假设不要求每一个串的权值仅仅能获取一次,那么直接跑来跑去的即可,可是由于仅仅能取一次,串非常少,
能够状态压缩DP,dp[i][j][k]记录前i个字符走到j状态而且已经获得的串状态为k时的最优解。滚动数组优化空间
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int inf=1e9+7;
const int maxn=1005;
int dp[2][maxn][1<<10];
int n,m,val[15];
struct Trie
{
int next[maxn][4],fail[maxn],_end[maxn];
int root,L;
int change(char ch)
{
if(ch=='A')return 0;
else if(ch=='T')return 1;
else if(ch=='G')return 2;
return 3;
}
int newnode()
{
for(int i=0; i<4; i++)
{
next[L][i]=-1;
}
_end[L++]=0;
return L-1;
}
void init()
{
L=0;
root=newnode();
}
void Insert(char *buf,int id)
{
int len=strlen(buf);
int now=root;
for(int i=0; i<len; i++)
{
int q=change(buf[i]);
if(next[now][q]==-1)
next[now][q]=newnode();
now=next[now][q];
}
_end[now]|=(1<<id);
}
void build()
{
queue<int>Q;
fail[root]=root;
for(int i=0; i<4; i++)
{
if(next[root][i]==-1)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now=Q.front();
Q.pop();
_end[now]|=_end[fail[now]];
for(int i=0; i<4; i++)
{
if(next[now][i]==-1)
{
next[now][i]=next[fail[now]][i];
}
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int get(int s)
{
int ans=0;
for(int i=0; i<n; i++)
{
if(s&(1<<i))
ans+=val[i];
}
return ans;
}
void solve()
{
memset(dp,0,sizeof(dp));
dp[0][0][0]=1;
for(int i=1; i<=m; i++)
{
memset(dp[i&1],0,sizeof(dp[i&1]));
for(int j=0; j<L; j++)
{
for(int k=0; k<4; k++)
{
int x=next[j][k];
for(int r=0; r<(1<<n); r++)
{
if(dp[(i+1)&1][j][r])
{
dp[i&1][x][r|_end[x]]=1;
}
}
}
}
}
int ans=-inf;
for(int j=0; j<(1<<n); j++)
{
for(int i=0; i<L; i++)
{
if(dp[m&1][i][j])
{
ans=max(ans,get(j));
}
}
}
if(ans<0)puts("No Rabbit after 2012!");
else printf("%d\n",ans);
}
} t;
char s[1005];
int main()
{
while(~scanf("%d%d",&n,&m))
{
t.init();
for(int i=0; i<n; i++)
{
int x;
scanf("%s%d",s,&val[i]);
t.Insert(s,i);
}
t.build();
t.solve();
}
return 0;
}

hdu 4057 AC自己主动机+状态压缩dp的更多相关文章

  1. poj 1699 Best Sequence(AC自己主动机+如压力DP)

    id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...

  2. HDU 2825 Wireless Password ( Trie图 && 状态压缩DP )

    题意 : 输入n.m.k意思就是给你 m 个模式串,问你构建长度为 n 至少包含 k 个模式串的方案有多少种 分析 : ( 以下题解大多都是在和 POJ 2778 && POJ 162 ...

  3. HDU 5418 Victor and World (状态压缩dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000 ...

  4. hdu 5067 Harry And Dig Machine (状态压缩dp)

    题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...

  5. HDU 4649 Professor Tian(反状态压缩dp,概率)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 初始有一个数字A0, 然后给出A1,A2..An共n个数字,这n个数字每个数字分别有一 ...

  6. [AC自己主动机+状压dp] hdu 2825 Wireless Password

    题意: 给n.m,k ,再给出m个单词 问长度为n的字符串.至少在m个单词中含有k个的组成方案有多少种. 思路: 因为m最大是10,所以能够採取状压的思想 首先建立trie图,在每一个单词的结束节点标 ...

  7. hdu 4640 Island and study-sister(状态压缩dp)

    先处理前两个学长到达各个点所需要的最少时间,在计算前两个学长和最后一个学长救出所有学妹的最少时间. #include<stdio.h> #include<string.h> # ...

  8. HDU 3001 Travelling (三进制状态压缩 DP)

    题意:有 n 个city,能够选择任一城市作为起点,每一个城市不能訪问超过2次, 城市之间有权值,问訪问所有n个城市须要的最小权值. 思路:由于每一个城市能够訪问最多两次,所以用三进制表示訪问的状态. ...

  9. hdu 1565 方格取数(1) 状态压缩dp

    方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. Sitemesh3的使用及配置

    1 . Sitemesh 3 简介 Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术.参考:百度百科,相关类似技术:A ...

  2. Docker 安装命令

    curl -sSL https://get.daocloud.io/docker | sh

  3. yii框架网址解析问题

    转载请注明来自souldak,微博:@evagle 首先如果你在config/main.php里面没有配置urlManager的话,那么流程如下(摘自yii官网) 用户发出了访问 URL http:/ ...

  4. paip.输入法编程---输入法ATIaN历史记录 c823

    paip.输入法编程---输入法ATIaN历史记录 c823 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...

  5. HDOJ 4007 Dave【最大覆盖集】

    Dave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submis ...

  6. Swift 初见

    http://numbbbbb.gitbooks.io/-the-swift-programming-language-/chapter1/02_a_swift_tour.html 本页内容包括: 简 ...

  7. 利用java concurrent 包实现日志写数据库的并发处理

    一.概述 在很多系统中,往往需要将各种操作写入数据库(比如客户端发起的操作). 最简单的做法是,封装一个公共的写日志的api,各个操作中调用该api完成自己操作日志的入库.但因为入数据库效率比较低,如 ...

  8. (step8.2.6)hdu 1848(Fibonacci again and again——组合博弈)

    题目大意:输入3个整数m,n,p,分别表示3堆石头中的石头个数 解题思路: 1)斐波那契数列的第16个数fib[16] == 1597 2)(sg[m]^sg[n]^sg[p])   .一定要加括号, ...

  9. Xcode6使用storyboard在TabBarController上建立三个以上Item

    在Xcode5上做以上的操作没有问题,这次是要在Xcode6上实现之,特记录以备用. 首先新建一个storyboard文件.取名Custom.storyboard.拖动菜单添加一个TabBarComt ...

  10. PHP - 操作MySQL数据库

    第16章 PHP操作MySQL 学习要点: 1.PHP连接到MySQL 2.增删改查 3.其他常用函数 如果你已经具有了使用PHP.SQL和MySQL的丰富经验,现在就可以把所有这些技术组合在一起.P ...