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++ 安全字符串拼接
#include <stdio.h> #include <stdint.h> #include <stdarg.h> #if defined(__GNUC__) # ...
- JQuery中国省市区无刷新三级联动查询
之前有写过用<Ajax控件来实现中国的省市区无刷新查询> 今天用JQuery来实现,用Ajax控件和JQuery的优缺点就先不说了. 效果图如下: 下面来结合代码来详细说明一下如何用JQu ...
- pod setup》error: RPC failed; result=18, HTTP code = 200
Try reducing the postBuffer size in the remote repository config. Follow the steps below Go to remot ...
- 实验一补充内容 Java开发环境的熟悉-刘蔚然
本次实验 PSP时间统计 步骤 耗时百分比 需求分析 5% 设计 10% 代码实现 67% 测试 15% 分析总结 3%
- asp.net跳转页面的三种方法比较(转)
2006-10-20 14:32 [小 大] 来源: 博客园 评论: 0分享至: 百度权重查询 词库网 网站监控 服务器监控 SEO监控 手机游戏 iPhone游戏 今天老师讲了三种跳转页面的方法,现 ...
- An unknown Subversion error occurred. (code = 155037)
这是因为在svn更新时意外中断引起的. 我的解决办法:如果本地没有更改,只是单纯获取svn的项目,则另起一个文件夹,重新checkout: 如果是本地有更改,则复制到新的文件夹,重新update.
- windows下memcache安装及配置
1.安装memcached服务,链接为http://i.cnblogs.com/Files.aspx, 下载解压后放在一个文件夹下,在开始搜索中输入cmd, 进入cmd黑框,cd 路径,进入memca ...
- Ioc-Autofac实现自动的注入
在开发过程中,最郁闷的莫过于当你新增一个Service时,你需要对该Service进行注册,有的是使用代码注入,有的是XML配置注入,不管是哪种类型的注入,经常会出现开发人员忘记注入的情况. 于是我试 ...
- 发起post请求
string postUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo"; //string req ...
- Python模块 (xlsxwriter)
xlsxwriter是python中用来处理execl表格的库 参考