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):

  1 #include <iostream>

  2 #include <cstdio>

  3 #include <algorithm>

  4 #include <cstring>

  5 #include <queue>

  6 using namespace std;

  7 typedef long long LL;

  8 

  9 const int MAXN = 1024;

 10 const int MAXL = 110;

 11 const int size = 4;

 12 

 13 char str[] = "ATCG";

 14 

 15 struct Node {

 16     int val;

 17     Node *go[size], *fail;

 18 } StatePool[MAXN];

 19 int ncnt;

 20 

 21 void init() {

 22     memset(StatePool, 0, ncnt * sizeof(Node));

 23     ncnt = 0;

 24 }

 25 

 26 Node* new_node() {

 27     return &StatePool[ncnt++];

 28 }

 29 

 30 void insert(Node* root, char s[], int id) {

 31     Node *p = root;

 32     for(int i = 0; s[i]; ++i) {

 33         int idx = strchr(str, s[i]) - str;

 34         if(!p->go[idx]) p->go[idx] = new_node();

 35         p = p->go[idx];

 36     }

 37     p->val |= (1 << id);

 38 }

 39 

 40 queue<Node*> que;

 41 void makeFail(Node* root) {

 42     root->fail = root;

 43     que.push(root);

 44     while(!que.empty()) {

 45         Node *tmp = que.front(); que.pop();

 46         for(int i = 0; i < size; ++i) {

 47             if(!tmp->go[i]) {

 48                 tmp->go[i] = tmp == root ? root : tmp->fail->go[i];

 49                 //if(tmp->go[i] == NULL) puts("error");

 50             } else {

 51                 Node *q = tmp->go[i];

 52                 q->fail = tmp == root ? root : tmp->fail->go[i];

 53                 q->val |= q->fail->val;

 54                 que.push(tmp->go[i]);

 55             }

 56         }

 57     }

 58 }

 59 

 60 bool dp[2][MAXN][MAXN], (*pre)[MAXN], (*now)[MAXN];

 61 char s[MAXN];

 62 int weight[MAXN];

 63 int n, L;

 64 

 65 inline int encode(Node *p) {

 66     return p - StatePool;

 67 }

 68 

 69 inline Node* decode(int idx) {

 70     return &StatePool[idx];

 71 }

 72 

 73 int solve(Node *root) {

 74     pre = dp[0], now = dp[1];

 75     memset(now, 0, sizeof(dp[0]));

 76     now[encode(root)][0] = true;

 77     for(int _ = 0; _ < L; ++_) {

 78         swap(pre, now);

 79         memset(now, 0, sizeof(dp[0]));

 80         for(int i = 0; i < ncnt; ++i) {

 81             Node *p = decode(i);

 82             for(int st = 0; st < (1 << n); ++st) if(pre[i][st]) {

 83                 for(int k = 0; k < size; ++k)

 84                     now[encode(p->go[k])][st | p->go[k]->val] = true;

 85             }

 86         }

 87     }

 88     int res = -1;

 89     for(int i = 0; i < ncnt; ++i) {

 90         for(int st = 0; st < (1 << n); ++st) if(now[i][st]) {

 91             int t = 0;

 92             for(int j = 0; j < n; ++j)

 93                 if((st >> j) & 1) t += weight[j];

 94             res = max(res, t);

 95         }

 96     }

 97     return res;

 98 }

 99 

100 int main() {

101     while(scanf("%d%d", &n, &L) != EOF) {

102         init();

103         Node *root = new_node();

104         for(int i = 0; i < n; ++i) {

105             scanf("%s%d", s, &weight[i]);

106             insert(root, s, i);

107         }

108         makeFail(root);

109         int res = solve(root);

110         if(res >= 0) printf("%d\n", res);

111         else puts("No Rabbit after 2012!");

112     }

113 }
View Code

 

你可能感兴趣的:(AC自动机)