ZOJ 3545 Rescue the Rabbit(AC自动机+状压DP)(The 2011 ACM-ICPC Asia Dalian Regional Contest)
2014-09-04 18:20
791 查看
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.
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.
题目大意:给n个串,每个串有一个权值,若一个串包含这些串,就把权值累加起来。问一个长度为L的串权值最多为多少。
思路:http://blog.sina.com.cn/s/blog_7da04dd30100xnux.html
代码(460MS):
View Code
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 = 1024; const int MAXL = 110; const int size = 4; char str[] = "ATCG"; struct Node { int val; Node *go[size], *fail; } StatePool[MAXN]; int ncnt; void init() { memset(StatePool, 0, ncnt * sizeof(Node)); ncnt = 0; } Node* new_node() { return &StatePool[ncnt++]; } void insert(Node* root, char s[], int id) { Node *p = root; for(int i = 0; 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 |= (1 << 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 = 0; 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[2][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[0], now = dp[1]; memset(now, 0, sizeof(dp[0])); now[encode(root)][0] = true; for(int _ = 0; _ < L; ++_) { swap(pre, now); memset(now, 0, sizeof(dp[0])); for(int i = 0; i < ncnt; ++i) { Node *p = decode(i); for(int st = 0; st < (1 << n); ++st) if(pre[i][st]) { for(int k = 0; k < size; ++k) now[encode(p->go[k])][st | p->go[k]->val] = true; } } } int res = -1; for(int i = 0; i < ncnt; ++i) { for(int st = 0; st < (1 << n); ++st) if(now[i][st]) { int t = 0; for(int j = 0; j < n; ++j) if((st >> j) & 1) 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 = 0; i < n; ++i) { scanf("%s%d", s, &weight[i]); insert(root, s, i); } makeFail(root); int res = solve(root); if(res >= 0) printf("%d\n", res); else puts("No Rabbit after 2012!"); } }
View Code
相关文章推荐
- Hdu 4057 Rescue the Rabbit (AC自动机+状态压缩dp) - 2011 ACM-ICPC Dalian Regional Contest Problem G
- Zoj 3541 The Last Puzzle (dp) - 2011 ACM-ICPC Dalian Regional Contest Problem C
- Zoj 3543 Number String (dp) - 2011 ACM-ICPC Dalian Regional Contest Problem E
- The 2011 ACM-ICPC Asia Dalian Regional Contest——D Hexadecimal View
- zoj 3545 - Rescue the Rabbit(AC自动机+dp)
- Zoj 3545 Rescue the Rabbit(ac自动机+dp)
- The 2011 ACM-ICPC Asia Dalian Regional Contest-D Hexadecimal View 【模拟】
- The 2016 ACM-ICPC Asia Dalian Regional Contest
- zoj 3418 || The 2010 ACM-ICPC Asia Chengdu Regional Contest - C Binary Number
- UVALive 7061 Dire Wolf (dp)(The 2014 ACM-ICPC Asia Beijing Regional Contest D)
- hdu 4057 Rescue the Rabbit(AC自动机+状压dp)
- HDU 4057 Rescue the Rabbit 【AC自动机+状压DP】
- The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest(套题HDU4001-4010)
- zoj 3829 Known Notation The 2014 ACM-ICPC Asia Mudanjiang Regional Contest K 贪心
- 回文自动机 + DFS --- The 2014 ACM-ICPC Asia Xi’an Regional Contest Problem G.The Problem to Slow Down You
- HDU 5045 Contest 期望+状压dp 2014 ACM/ICPC Asia Regional Shanghai Online
- HDU4057 Rescue the Rabbit(AC自动机+状压DP)
- ZOJ 3819 Average Score The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
- The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
- 2011 大连网络赛 The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest 解题报告