HDU3341--Lost's revenge

来源:互联网 发布:淘宝触摸屏版 编辑:程序博客网 时间:2024/06/18 18:28
Problem 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.
 

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.
 

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
3ACCGGTCGAT1AAAAA0
 

Sample Output
Case 1: 3Case 2: 2
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 528#define LL long long intint first,rear,Cnt;LL dp[15008][528];int num[4];int Hash[4];inline LL max(LL a,LL b){return a>b?a:b;}struct node{node * fail;node * next[4];int cnt,end;node(){cnt = Cnt++;end = 0;for(int i = 0;i < 4;i++){next[i] = NULL;}}}*q[maxn],*qq[maxn];int getid(char a){switch(a){case('A'):return 0;case('G'):return 1;case('C'):return 2;case('T'):return 3;}}void insert(char * s,node * root){node * p = root;int len = strlen(s);for(int i = 0;i < len;i++){int id = getid(s[i]);if(p -> next[id] == NULL){p -> next[id] = qq[Cnt-1] = new node();}p = p -> next[id];}p -> end++;}void build_ac_automation(node * root){node * p = NULL;q[rear++] = root;while(first < rear){p = q[first++];for(int i = 0;i < 4;i++){if(p -> next[i] != NULL){if(p == root){p -> next[i] -> fail = root;}else{p -> next[i] -> fail = p -> fail -> next[i];p -> next[i] -> end += p -> fail -> next[i] -> end;//这里的+=要好好体会。}q[rear++] = p -> next[i];}else{if(p == root){p -> next[i] = root;}else{p -> next[i] = p -> fail -> next[i];}}}}}void init(){Hash[0] = 1;Hash[1] = Hash[0] * (num[0] + 1);Hash[2] = Hash[1] * (num[1] + 1);Hash[3] = Hash[2] * (num[2] + 1);}int main(){//freopen("in.txt","r",stdin);int n,cas = 0;char str[12],ans[42];while(scanf("%d",&n)!=EOF && n){cas++;first = rear = Cnt = 0;node * root;qq[0] = root = new node();for(int i = 0;i < n;i++){scanf("%s",str);insert(str,root);}build_ac_automation(root);memset(dp,-1,sizeof(dp));memset(num,0,sizeof(num));scanf("%s",ans);int len = strlen(ans);for(int i = 0;i < len;i++){int id = getid(ans[i]);num[id]++;}init();dp[0][0] = 0;int t = num[0]*Hash[0] + num[1]*Hash[1] + num[2]*Hash[2] + num[3]*Hash[3];for(int i = 0;i < t;i++){for(int j = 0;j < Cnt;j++){int num3 = i/Hash[3];int num2 = (i-num3*Hash[3])/Hash[2];int num1 = (i-num3*Hash[3]-num2*Hash[2])/Hash[1];int num0 = (i-num3*Hash[3]-num2*Hash[2]-num1*Hash[1])/Hash[0];if(dp[i][j] == -1)continue;if(num0 < num[0])dp[i+Hash[0]][qq[j] -> next[0] -> cnt] = max(dp[i+Hash[0]][qq[j] -> next[0] -> cnt],dp[i][j] + qq[j] -> next[0] -> end);if(num1 < num[1])dp[i+Hash[1]][qq[j] -> next[1] -> cnt] = max(dp[i+Hash[1]][qq[j] -> next[1] -> cnt],dp[i][j] + qq[j] -> next[1] -> end);if(num2 < num[2])dp[i+Hash[2]][qq[j] -> next[2] -> cnt] = max(dp[i+Hash[2]][qq[j] -> next[2] -> cnt],dp[i][j] + qq[j] -> next[2] -> end);if(num3 < num[3])dp[i+Hash[3]][qq[j] -> next[3] -> cnt] = max(dp[i+Hash[3]][qq[j] -> next[3] -> cnt],dp[i][j] + qq[j] -> next[3] -> end);}}LL Ans  = 0;for(int i = 0;i < Cnt;i++){Ans = max(Ans,dp[t][i]);}printf("Case %d: %I64d\n",cas,Ans);}return 0;}


原创粉丝点击