首页 > 代码库 > HDU - 3341 Lost's revenge(AC自己主动机+DP)
HDU - 3341 Lost's revenge(AC自己主动机+DP)
Description
Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talent in the
field of number theory. So Lost had never won the game. He was so ashamed and angry, but he didn‘t know how to improve his level of number theory.
One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I‘m Spring Brother, and I saw AekdyCoin shames you again and again. I can‘t bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".
It‘s soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I‘m Spring Brother, and I saw AekdyCoin shames you again and again. I can‘t bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".
It‘s soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
Input
There are less than 30 testcases.
For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
The last line is Lost‘s gene sequences, its length is also less or equal 40.
All genes and gene sequences are only contains capital letter ACGT.
For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
The last line is Lost‘s gene sequences, its length is also less or equal 40.
All genes and gene sequences are only contains capital letter ACGT.
Output
For each testcase, output the case number(start with 1) and the most "level of number theory" with format like the sample output.
Sample Input
3 AC CG GT CGAT 1 AA AAA 0
Sample Output
Case 1: 3 Case 2: 2
思路:如果ACGT的个数依次是num[0],num[1],num[2],num[3],将这四个数压缩成一个数,降低内存,然后动归dp[i][status]表示到自己主动机上i状态此时ACGT个数的状态时的最少
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int inf = 0x3f3f3f3f; struct Trie { int nxt[510][4], fail[510], end[510]; int root, cnt; int newNode() { for (int i = 0; i < 4; i++) nxt[cnt][i] = -1; end[cnt++] = 0; return cnt - 1; } void init() { cnt = 0; root = newNode(); } int getch(char ch) { if (ch == ‘A‘) return 0; else if (ch == ‘C‘) return 1; else if (ch == ‘G‘) return 2; return 3; } void insert(char str[]) { int len = strlen(str); int now = root; for (int i = 0; i < len; i++) { if (nxt[now][getch(str[i])] == -1) nxt[now][getch(str[i])] = newNode(); now = nxt[now][getch(str[i])]; } end[now]++; } void build() { queue<int> q; fail[root] = root; for (int i = 0; i < 4; i++) { if (nxt[root][i] == -1) nxt[root][i] = root; else { fail[nxt[root][i]] = root; q.push(nxt[root][i]); } } while (!q.empty()) { int now = q.front(); q.pop(); end[now] += end[fail[now]]; //好像每次都是这里 for (int i = 0; i < 4; i++) { if (nxt[now][i] == -1) nxt[now][i] = nxt[fail[now]][i]; else { fail[nxt[now][i]] = nxt[fail[now]][i]; q.push(nxt[now][i]); } } } } int dp[510][11*11*11*11+5]; int bit[4], num[4]; int solve(char str[]) { int len = strlen(str); memset(num, 0, sizeof(num)); for (int i = 0; i < len; i++) num[getch(str[i])]++; bit[0] = (num[1]+1) * (num[2]+1) * (num[3]+1); bit[1] = (num[2]+1) * (num[3]+1); bit[2] = (num[3]+1); bit[3] = 1; memset(dp, -1, sizeof(dp)); dp[root][0] = 0; for (int A = 0; A <= num[0]; A++) for (int B = 0; B <= num[1]; B++) for (int C = 0; C <= num[2]; C++) for (int D = 0; D <= num[3]; D++) { int s = A * bit[0] + B * bit[1] + C * bit[2] + D * bit[3]; for (int i = 0; i < cnt; i++) if (dp[i][s] >= 0) { for (int k = 0; k < 4; k++) { if (k == 0 && A == num[0]) continue; if (k == 1 && B == num[1]) continue; if (k == 2 && C == num[2]) continue; if (k == 3 && D == num[3]) continue; dp[nxt[i][k]][s+bit[k]] = max(dp[nxt[i][k]][s+bit[k]], dp[i][s]+end[nxt[i][k]]); } } } int ans = 0; int status = num[0] * bit[0] + num[1] * bit[1] + num[2] * bit[2] + num[3] * bit[3]; for (int i = 0; i < cnt; i++) ans = max(ans, dp[i][status]); return ans; } } ac; char str[50]; int main() { int n, cas = 1; while (scanf("%d", &n) != EOF && n) { ac.init(); for (int i = 0; i < n; i++) { scanf("%s", str); ac.insert(str); } ac.build(); scanf("%s", str); printf("Case %d: %d\n", cas++, ac.solve(str)); } return 0; }
HDU - 3341 Lost's revenge(AC自己主动机+DP)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。