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!");
}
}
上一篇:【Python3练习题 005】输入三个整数x,y,z,请把这三个数由小到大输出


下一篇:VS2013编译google protobuf 出现问题error C3861: “min”: