ZOJ 3545 Rescue the Rabbit(AC自动机+状压DP)(The 2011 ACM-ICPC Asia Dalian Regional Contest)
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!".
题目大意:给n个串,每个串有一个权值,若一个串包含这些串,就把权值累加起来。问一个长度为L的串权值最多为多少。
思路:http://blog.sina.com.cn/s/blog_7da04dd30100xnux.html
代码(460MS):
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXL = ;
const int size = ; char str[] = "ATCG"; struct Node {
int val;
Node *go[size], *fail;
} StatePool[MAXN];
int ncnt; void init() {
memset(StatePool, , ncnt * sizeof(Node));
ncnt = ;
} Node* new_node() {
return &StatePool[ncnt++];
} void insert(Node* root, char s[], int id) {
Node *p = root;
for(int i = ; s[i]; ++i) {
int idx = strchr(str, s[i]) - str;
if(!p->go[idx]) p->go[idx] = new_node();
p = p->go[idx];
}
p->val |= ( << id);
} queue<Node*> que;
void makeFail(Node* root) {
root->fail = root;
que.push(root);
while(!que.empty()) {
Node *tmp = que.front(); que.pop();
for(int i = ; i < size; ++i) {
if(!tmp->go[i]) {
tmp->go[i] = tmp == root ? root : tmp->fail->go[i];
//if(tmp->go[i] == NULL) puts("error");
} else {
Node *q = tmp->go[i];
q->fail = tmp == root ? root : tmp->fail->go[i];
q->val |= q->fail->val;
que.push(tmp->go[i]);
}
}
}
} bool dp[][MAXN][MAXN], (*pre)[MAXN], (*now)[MAXN];
char s[MAXN];
int weight[MAXN];
int n, L; inline int encode(Node *p) {
return p - StatePool;
} inline Node* decode(int idx) {
return &StatePool[idx];
} int solve(Node *root) {
pre = dp[], now = dp[];
memset(now, , sizeof(dp[]));
now[encode(root)][] = true;
for(int _ = ; _ < L; ++_) {
swap(pre, now);
memset(now, , sizeof(dp[]));
for(int i = ; i < ncnt; ++i) {
Node *p = decode(i);
for(int st = ; st < ( << n); ++st) if(pre[i][st]) {
for(int k = ; k < size; ++k)
now[encode(p->go[k])][st | p->go[k]->val] = true;
}
}
}
int res = -;
for(int i = ; i < ncnt; ++i) {
for(int st = ; st < ( << n); ++st) if(now[i][st]) {
int t = ;
for(int j = ; j < n; ++j)
if((st >> j) & ) t += weight[j];
res = max(res, t);
}
}
return res;
} int main() {
while(scanf("%d%d", &n, &L) != EOF) {
init();
Node *root = new_node();
for(int i = ; i < n; ++i) {
scanf("%s%d", s, &weight[i]);
insert(root, s, i);
}
makeFail(root);
int res = solve(root);
if(res >= ) printf("%d\n", res);
else puts("No Rabbit after 2012!");
}
}
ZOJ 3545 Rescue the Rabbit(AC自动机+状压DP)(The 2011 ACM-ICPC Asia Dalian Regional Contest)的更多相关文章
- hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)
标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- hdu2825 Wireless Password(AC自动机+状压dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***
http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...
- HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )
模板来自notonlysuccess. 模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩. 将模式串加入AC自动机,最多有10*100个状态. dp[i][j][k]:串长为i,在T ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
随机推荐
- C/C++程序编译流程(预处理->编译->汇编->链接)
程序的基本流程如图: 1. 预处理 预处理相当于根据预处理指令组装新的C/C++程序.经过预处理,会产生一个没有宏定义,没有条件编译指令,没有特殊符号的输出文件,这个文件的含义同原本的文件无异,只是内 ...
- node express 学习2
上次我们的express已经安装好了 接下来我们修改渲染引擎为html // view engine setup app.set('views', path.join(__dirname, 'view ...
- 解决ArcGIS API for Silverlight 加载地图的内外网访问问题
原文:解决ArcGIS API for Silverlight 加载地图的内外网访问问题 先上一个类,如下: public class BaseClass { public static string ...
- json的eval为什么要用msg.d
在做一个关于搜索功能时用到了jquery autocomplete,发现返回数据时都用到了一个.d,比如: var datas = eval('(' + msg.d + ')'); 这个.d是什么呢, ...
- Anacodna之conda与 virtualenv对比使用教程,创建虚拟环境
conda创建虚拟环境 1.查看包 conda list查看安装了哪些包 conda env list查看有哪些虚拟环境 conda -V查看conda的版本 2.创建虚拟环境,命名为myflaska ...
- leetcode:Valid Parentheses
括号匹配 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- html5添加音乐包括暂停
<audio id="musicfx" loop="loop" autoplay="autoplay"> <source ...
- SET Statements (Transact-SQL)
The Transact-SQL programming language provides several SET statements that change the current sessio ...
- 基础拾掇之——http基础
基础拾掇之——http基础 http协议介绍 http:Hyper Text Transfer Protocol 超文本传输协议,是互联网应用最为广泛的一种网络协议,主要用于Web服务.通过计算机处理 ...
- python深复制和浅复制
深复制:一个更改后不会影响到其他的变量,另一个变量B赋值给变量A时,虽然A和B的内存空间仍然相同,但当A的值发生变化时,会重新给A分配空间,A和B的地址变得不再相同 浅复制:改变一个就会引起另一个的改 ...