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)的更多相关文章

  1. hdu 4057--Rescue the Rabbit(AC自动机+状压DP)

    题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...

  2. zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)

    Time Limit: 10 Seconds      Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...

  3. Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

    标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...

  4. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】

    题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...

  6. HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解

    题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...

  7. hdu2825 Wireless Password(AC自动机+状压dp)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  8. HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...

  9. HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )

    模板来自notonlysuccess. 模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩. 将模式串加入AC自动机,最多有10*100个状态. dp[i][j][k]:串长为i,在T ...

  10. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

随机推荐

  1. HBase学习笔记-基础(一)

    HBase版本:0.97 1.Get Gets实在Scan的基础上实现的. 2.联合查询(Join) HBase是否支持联合是一个网上常问问题.简单来说 : 不支持.至少不像传统RDBMS那样支持. ...

  2. hiveserver2以及beeline客户端的使用

    一:开启服务 1.启动前端的hiveserver2 按住ctrl+c就可以结束这个服务. 2.怎么知道已经开启的服务 如果进程在后台,可以查出来,kill这些进程. 3.后端开启服务 二:beelin ...

  3. Qt工具知多少(一目了然)

    一级题目: Qt Designer — 所见即所得的界面设计工具, 可以用拖拽的方式将控件排布在界面上,支持layout, 支持signal/slot编辑. 生成的文件保存为ui格式, ui是xml格 ...

  4. 利用快速排序原理找出数组中前n大的数

    #include <stdio.h> #include <stdint.h> #include <stdlib.h> #define MAX_SIZE 400001 ...

  5. PresentViewController切换界面(一些系统自带的页面切换动画)

    视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...

  6. 数据库.mdf

    对于.mdf文件和.ldf数据库文件, 首先打开SQL Server Management Studio Express,登陆上后,右键点击数据库,附加->选择目标文件就可以了.

  7. Java学习-008-判断文件类型实例

    此文源码主要为应用 Java 如何判断文件类型的源码及其测试源码.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-2 23:02:00,请知悉. Java 判断文件类型源码如 ...

  8. iOS 应用程序的生命周期(转CocoaChina)

    对于iOS应用程序,关键是要知道你的应用程序是否正在前台或后台运行.由于系统资源在iOS设备上较为有限,一个应用程序必须在后台与前台有不同的行为.操作系统也会限制你的应用程序在后台的运行,以提高电池寿 ...

  9. toggle笔记

    <!DOCTYPE html> <!-- saved from url=(0040)http://v3.bootcss.com/examples/carousel/ --> & ...

  10. MySQL 部分函数使用

    1.DATE_ADD 参考博客:MySQL日期时间函数大全 转 例:DATE_ADD(date,INTERVAL expr type) 2.日期转字符串 DATE_FORMAT 参考博客:MYSQL中 ...