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 ...
随机推荐
- js 和 jquery 获取页面和滚动条的高度 视口高度文档高度
js 和 jquery 获取页面和滚动条的高度 //页面位置及窗口大小 function GetPageSize() { var scrW, scrH; if(window.innerHeight & ...
- yum change source repo centos共存安装sun jdk6和jdk7
之前一直使用的是163的源,今天从微博看到阿里云推出了自己的源.因为我的主机是阿里云,所以可以走内网,速度提升更快.过程如下:cd /etc/yum.repos.d/mv mv CentOS-Base ...
- Hadoop学习笔记(一)
HDFS适合一次写入,多次读取NameNode将文件系统的元数据存储在内存中,因此HDFS所能存储的文件总数受限于NameNode容量类:IOUtil Progressable URL.setURLS ...
- 使用JetBrains dotMemory 4.0分析内存
安装下载地址:http://www.jetbrains.com/profiler/ 1.在本地启动web应用后,打开dotMemory,附加进程 2.附加后会看到集中颜色得粗条,不断往左边走动,这是内 ...
- 【转】MySQL USE NAMES 'UTF8'
先说MySQL的字符集问题.Windows下可通过修改my.ini内的 # CLIENT SECTION [mysql] default-character-set=utf8 # SERVER SEC ...
- SQL SERVER中非聚集索引的覆盖,连接,交叉,过滤
1.覆盖索引:select和where中包含的结果集中应存在“非聚集索引列”,这样就不用查找基表了,索引表即可搞定: 2.索引交叉:索引的交叉可以理解成建立多个非聚集索引之间的join,如表实体一 ...
- WPFFontCache_v0400.exe CPU使用率过高的问题
最近的电脑很慢 CPU超过50%了 任务管理器显示是WPFFontCache_v0400.exe 的问题 每次强制终止后不就又重新启动很是麻烦, 在MSDN中找到了解决办法: 禁用Windows Pr ...
- CocoaPods的安装及使用/利用开源库Diplomat实现分享及第三方登录/git的使用
<<史上最简洁版本>> 1.gem sources -l查看 当前的源 //1.1 sudo -i..以下都是以管理员的身份来操作的 2.gem sources --remov ...
- Windows7下 配置 Apache + PHP + MySQL + Zend Studio配置
相关软件下载: Apache 版本:(httpd-2.2.25) PHP ...
- NYU Hand Pose Dataset
http://cims.nyu.edu/~tompson/NYU_Hand_Pose_Dataset.htm#overview