hdu 4057 Rescue the Rabbit
题意
给出n(n<=10)个串,每个串有个权值,然后让你构造一个长度为l(l<=100)的串,如果他包含给出的串就得到相应的权值,求可能得到的最大权值
解法
AC自动机+DP,很显然要建立自动机,然后在上面跑,如果不要求每个串的权值只能获取一次,那么直接跑来跑去的就行,但是因为只能取一次,串很少,可以状态压缩DP,dp[i][j][k]记录前i个字符走到j状态并且已经获得的串状态为k时的最优解。需要滚动数组优化空间--
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N = ;
const int inf = ~0u>>;
int hash[];
struct node{
node *ch[],*fail;
int mask;
void clear(){
for(int i = ;i < ;i++)ch[i] = NULL;fail = NULL;
mask = ;
}
};
node stk[N*];
bool dp[][N][];
int score[];
struct Trie{
node *root;int top;
node* newnode(){
node *p = &stk[top++];
p -> clear();
return p;
}
void init(){
top = ;
root = newnode();
}
void insert(char *s,int number){
node *p = root;int len = strlen(s);
for(int i = ;i < len;i++){
int id = hash[s[i]];
if(p -> ch[id] == NULL)
p -> ch[id] = newnode();
p = p -> ch[id];
}
p -> mask |= <<number;
}
void build(){
queue<node*> Q;
root -> fail = root;
for(int i = ;i < ;i++)
if(root -> ch[i] == NULL)
root -> ch[i] = root;
else{
Q.push(root -> ch[i]);
root -> ch[i] -> fail = root;
}
while(!Q.empty()){
node *p = Q.front();Q.pop();
for(int i = ;i < ;i++)
if(p -> ch[i] == NULL)
p -> ch[i] = p -> fail -> ch[i];
else{
Q.push(p -> ch[i]);
p -> ch[i] -> fail = p -> fail -> ch[i];
p -> ch[i] -> mask |= p -> ch[i] -> fail -> mask;
}
}
}
int solve(int len,int number){
memset(dp,,sizeof(dp));
dp[][][] = true;
for(int i = ;i <= len;i++){
memset(dp[i&],,sizeof(dp[i&]));
for(int j = ;j < top;j++){
node *cur = &stk[j];
for(int st = ;st < <<number;st++){
if(dp[(i+)&][j][st]){
for(int k = ;k < ;k++){
node *next = cur -> ch[k];
dp[i&][next - stk][st | next -> mask] = ;
}
}
}
}
}
int ans = -inf;
for(int i = ;i < top;i++)
for(int j = ;j < <<number;j++)
if(dp[len&][i][j]){
ans = max(ans,score[j]);
}
return ans;
}
};
Trie AC;
char s[N];
int w[];
int main(){
hash['A'] = ;hash['C'] = ;hash['G'] = ;hash['T'] = ;
int n,l;
int cas = ;
while(~scanf("%d%d",&n,&l)){
AC.init();
for(int i = ;i < n;i++){
scanf("%s%d",s,&w[i]);
AC.insert(s,i);
}
for(int i = ;i < <<n ;i++){
score[i] = ;
for(int j = ;j < n;j++)
if(i&(<<j))score[i] += w[j];
}
AC.build();
int ans = AC.solve(l,n);
//printf("Case %d\n",cas++);
if(ans < )puts("No Rabbit after 2012!");
else printf("%d\n",ans);
}
return ;
}
hdu 4057 Rescue the Rabbit的更多相关文章
- HDU 4057 Rescue the Rabbit(AC自动机+DP)
题目链接 一个数组开小了一点点,一直提示wa,郁闷,这题比上个题简单一点. #include <iostream> #include <cstring> #include &l ...
- HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )
模板来自notonlysuccess. 模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩. 将模式串加入AC自动机,最多有10*100个状态. dp[i][j][k]:串长为i,在T ...
- 【HDOJ】4057 Rescue the Rabbit
挺有意思的一道题目,解法是AC自动机+DP.AC自动机建立fail指针时,一定要注意结点的属性也需要传递.AC自动机结合了trie和kmp的优点.需要注意的是,每个模式串仅计算一次,否则这题很难解. ...
- hdu4057 Rescue the Rabbit
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4057 题目: Rescue the Rabbit Time Limit: 20000/10000 MS ( ...
- hdu 4057 AC自己主动机+状态压缩dp
http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...
- HDU-4057 Rescue the Rabbit(AC自动机+DP)
Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 1222 Wolf and Rabbit(gcd)
HDU 1222 Wolf and Rabbit (最大公约数)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- hdu4057 Rescue the Rabbit(AC自己主动机+DP)
Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- JSOI2009 密码 和 JSOI2007 文本生成器 和 ZOJ3545 Rescue the Rabbit
密码 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝试所有可能的字母组合,但这是一项很耗时又容易被发现的工 作.所以,为了获取对方的登陆口令,在 ...
随机推荐
- vue 自定义组件 v-model双向绑定、 父子组件同步通信【转】
父子组件通信,都是单项的,很多时候需要双向通信.方法如下: 1.父组件使用:msg.sync="aa" 子组件使用$emit('update:msg', 'msg改变后的值xxx ...
- 关于在Qt里让程序休眠一段时间的方法总结
出处:http://hanzhaoxin.cnblogs.com/ Qt 为何没有提供 Sleep 论坛上不时见到有人问: Qt 为什么没有提供跨平台的 sleep 函数? 使用平台相关的 Sleep ...
- 第1节 flume:4、离线项目处理的整个架构图;5、flume的基本介绍;
第1节 flume:4.离线项目处理的整个架构图 辅助系统工具:flume,azkaban,sqoop. 在一个完整的离线大数据处理系统中,除了hdfs+mapreduce+hive组成分析系统的核心 ...
- Java中的类加载器--Class loader
学习一下Java中的类加载器,这个是比较底层的东西,好好学习.理解一下. 一.类加载器的介绍 1.类加载器:就是加载类的工具,在java程序中用到一个类,java虚拟机首先要把这个类的字节码加载到内 ...
- 「 SPOJ GSS3 」 Can you answer these queries III
# 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段 ...
- InnoDB INFORMATION_SCHEMA FULLTEXT Index Tables
InnoDB INFORMATION_SCHEMA FULLTEXT Index Tables 下表提供了FULLTEXT索引的元数据: mysql> SHOW TABLES FROM INFO ...
- day15-python之变量和递归
1.局部变量与全局变量 #!/usr/bin/env python # -*- coding:utf-8 -*- # name='lhf' # def change_name(): # global ...
- Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 必看!
Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 觉得好的麻烦点下推荐!谢谢!
- style对象的cssText方法
cssText 本质是什么? cssText 的本质就是设置 HTML 元素的 style 属性值. cssText 怎么用? domElement.style.cssText = "col ...
- 【HIHOCODER 1604】股票价格II(堆)
描述 小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN. 在小Hi的策略中,每天可以在下列三种操作中选取一种: 1.什么也不做: 2.按照当天的价格买进一个单位的 ...