HDU 3341 Lost ' s revenge ac automaton +DP

Source: Internet
Author: User
Tags cas

Lost ' s revenge

Time limit:15000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 3757 Accepted Submission (s): 1020


Problem Descriptionlost and Aekdycoin are friends. They always play "number Game" (A boring game based on number theory) together. We all know that aekdycoin are the man called "nuclear weapon of fzu,descendant of Jingrun", because of he talent in the F Ield of number theory. So Lost had never won the game. He is so ashamed and angry, but he didn ' t know how to improve his level of number theory.

One noon, when Lost were 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 your a chance to rearrange your gene sequences to defeat aekdycoin! ".

It ' s soooo crazy and unbelievable to rearrange the gene sequences, but Lost have no choice. He knows some genes called "number theory gene" would affect one "level of number Theory". And both of the same kind of gene in different position in the gene sequences would affect the "level of number theory", Eve n though they overlap each other. There is nothing but revenge in his mind. So he needs you and calculate the most "level of number Theory" after rearrangement.

Inputthere is less than and 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 the Every "number theory gene" is no more than 10.
The last line was Lost ' s gene sequences, its length was also less or equal 40.
All genes and gene sequences is only contains capital letter ACGT.

Outputfor each testcase, output the case number (start with 1) and the most ' level of number theory ' with format like the S Ample output. Sample Input3accggtcgat1aaaaa0

Sample OutputCase 1:3case 2:2

/*hdu 3341 Lost ' s revenge ac automaton +DP give n substrings and a string in the STR,STR position can be arbitrarily adjusted. The maximum number of substrings that can be included is obviously using DP, but there is a problem when saving the state. Because there are agct four, then the largest need for memory is 40*40*40*40 ultra-memory but in fact, the total length of STR is 40, that is, the number of a+c+g+t is 40, then the largest need for memory is 10*10*10*10 so dp[i][j] to save the current node as I   , the maximum value of AGCT is the state of J when used; DP is the mishap tat and then the repeating substring is also repeated calculation. hhh-2016-04-27 22:11:06*/#include <iostream> #include <vector> #include <cstring> #include < string> #include <cstdio> #include <queue> #include <algorithm> #include <functional># Include <map>using namespace std; #define Lson (i<<1) #define Rson ((i<<1) | |) typedef unsigned LONG Long ll;typedef unsigned int ul;const int mod = 20090717;const int INF = 0x3f3f3f3f;const int N = 505;int tot;int n;int dp[n][1    1*11*11*11+10];int tal[10];int num[4];int can (int id,int wt) {if (WT = = 0) return id/1000000;    else if (wt = = 1) return id/10000%100;    else if (WT = = 2) return id/100%100; else if (WT = = 3) return id%100;} struct tire{int nex[n][4],fail[n],eD[n];    int root,l;        int NewNode () {for (int i = 0; i < 4; i++) nex[l][i] = 1;        ed[l++] = 0;    return L-1;    } void Ini () {L = 0,root = NewNode ();        } int cal (char ch) {if (ch = = ' A ') return 0;        else if (ch = = ' C ') return 1;        else if (ch = = ' G ') return 2;    else if (ch = = ' T ') return 3;        } void Inser (char buf[]) {int len = strlen (BUF);        int now = root;            for (int i = 0; i < len; i++) {int ta = cal (Buf[i]);            if (nex[now][ta] = =-1) nex[now][ta] = NewNode ();        now = Nex[now][ta];    } ed[now]++;        } void Build () {Queue<int >q;        Fail[root] = root;            for (int i = 0; i < 4; i++) if (nex[root][i] = = 1) nex[root][i] = root;                else {Fail[nex[root][i]] = root; Q.push (nex[Root][i]);            } while (!q.empty ()) {Int. now = Q.front ();            Q.pop ();            Ed[now] + = Ed[fail[now]]; for (int i = 0; i < 4; i++) {if (nex[now][i] = = 1) nex[now][i] = nex[fail[                Now]][i];                    else {Fail[nex[now][i]] = nex[fail[now]][i];                Q.push (Nex[now][i]);        }}}} void solve (int len) {memset (dp,-1,sizeof (DP));        Dp[0][0] = 0;        Num[0] = (tal[3]+1) * (tal[1]+1) * (1+tal[2]);        NUM[1] = (tal[2]+1) * (tal[3]+1);        NUM[2] = tal[3]+1;        NUM[3] = 1;  for (int t0 = 0; t0 <= tal[0], t0++) for (int t1 = 0, T1 <= tal[1]; t1++) for (int t2 = 0; T2 <= tal[2];                        t2++) for (int t3 = 0, T3 <= tal[3]; t3++) for (int i = 0; i < L; i++) {inT tn = t1*num[1]+t2*num[2]+t3+t0*num[0];                                    if (Dp[i][tn] >= 0) for (int k = 0; k < 4; k++) {                                    int ta = nex[i][k];                                    if (k = = 0 && T0 = = tal[0]) continue;                                    if (k = = 1 && T1 = = tal[1]) continue;                                    if (k = = 2 && t2 = = tal[2]) continue;                                    if (k = = 3 && T3 = = tal[3]) continue;                                Dp[ta][tn+num[k]] = max (Dp[ta][tn+num[k]],dp[i][tn] + ed[ta]);        }} int ans = 0;        int ta = num[0]*tal[0]+num[1]*tal[1]+num[2]*tal[2]+num[3]*tal[3];        for (int i =0; i < L; i++) {ans = max (Ans,dp[i][ta]);    } printf ("%d\n", ans); }};    Tire Ac;char Buf[50];int Main () {int cas = 1; while (scanf ("%d", &Amp;n) ==1 && N) {Ac.ini ();            for (int i = 0; i < n; i++) {scanf ("%s", buf);        Ac.inser (BUF);        } ac.build ();        scanf ("%s", buf);        memset (Tal,0,sizeof (TAL));        for (int i = 0; i < (int) strlen (BUF); i++) {tal[ac.cal (buf[i])]++;        } printf ("Case%d:", cas++);    Ac.solve (strlen (BUF)); } return 0;}

  

HDU 3341 Lost ' s revenge ac automaton +DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.