HDU 4057 Rescue the Rabbit (AC automation + DP)

Source: Internet
Author: User

HDU 4057 Rescue the Rabbit (AC automation + DP)

 

Rescue the RabbitTime Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1482    Accepted Submission(s): 430


Problem Description 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 participant gene segment, we cocould 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 wocould 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 large 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 participating gene segment and the length of rabbits 'genes.

The next n lines each line contains a string dna I and an integer wi (| wi | ≤ 100), indicating this gene segment and the value it can contriating 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 shoshould output No Rabbit after 2012 !.

Sample Input
2 4ATG 4TGC -31 6TGC 44 1A -1T -2G -3C -4

Sample Output
44No Rabbit after 2012!Hintcase 1:we can find a rabbit whose gene string is ATGG(4), or ATGA(4) etc.case 2:we can find a rabbit whose gene string is TGCTGC(4), or TGCCCC(4) etc.case 3:any gene string whose length is 1 has a negative W. 

Author HONG, Qize
Source 2011 Asia Dalian Regional Contest

 

Question:

There are four letters of atcg, and a string with a length of l is required. n mode strings and their weights are given. Ask the maximum and maximum values of the strings that can be constructed, each mode string is calculated only once.

Analysis:

It is easy to think of as an AC automatic machine. We can find a feasible solution with the length of l on the AC automatic machine and find the maximum value. When dp [I] [j] [k] is used to indicate that the string length is I, whether the j nodes on the AC automatic machine can reach the status k. Because there are only 10 Mode strings, so there are a maximum of 2 ^ 10 status, the pressure on the line, then the length of the string for I is changed from the first I-1, therefore, you can use a scrolling array.

 

 

/* * * Author : fcbruce 
 
   * * Time : Thu 13 Nov 2014 08:23:41 PM CST * */#include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            
             #include 
             
              #include 
              
               #include
               #include 
                
                 #define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#ifdef _WIN32 #define lld %I64d#else #define lld %lld#endif#define maxm #define maxn 1007using namespace std;char gene[17][233];int w[17];int dp[2][maxn][2333];int q[maxn<<1];const int maxsize = 4;struct ACauto{ int ch[maxn][maxsize]; int val[maxn],last[maxn],nex[maxn]; int sz; ACauto() { sz=1; val[0]=0; memset(ch[0],0,sizeof ch[0]); } void clear() { sz=1; val[0]=0; memset(ch[0],0,sizeof ch[0]); } int idx(char c) { switch (c) { case 'A': return 0; case 'T': return 1; case 'C': return 2; case 'G': return 3; } } void insert(const char *s,int v) { int u=0; for (int i=0;s[i]!='';i++) { int c=idx(s[i]); if (ch[u][c]==0) { memset(ch[sz],0,sizeof ch[sz]); val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=v; } void get_fail() { int f=0,r=-1; nex[0]=0; for (itn c=0;c
                 
                  =0) printf(%d,MAX); else puts(No Rabbit after 2012!); } return 0;}
                 
                
              
             
            
           
          
         
        
       
      
     
    
   
  
 


 

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.