Hdu5375 Gray code (Dynamic Planning)

Source: Internet
Author: User

Hdu5375 Gray code (Dynamic Planning)

Question:

 

Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 860 Accepted Submission (s): 490



Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit ). the reflected binary code was originally designed to prevent spurous output from electromechanical switches. today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.



Now, you are given a binary number of length n including '0', '1' and '? '(? Means that you can use either 0 or 1 to fill this position) and n integers (a1, a2 ,...., An). A certain binary number corresponds to a gray code only. If the ith bit of this gray code is 1, you can get the point ai.
Can you tell me how many points you can get at most?

For instance, the binary number "00? 0 "may be" 0000 "or" 0010 ", and the corresponding gray code are" 0000 "or" 0011 ". you can choose "0000" getting nothing or "0011" getting the point a3 and a4.

Input The first line of the input contains the number of test cases T.

Each test case begins with string with '0', '1' and '? '.

The next line contains n (1 <= n <= 200000) integers (n is the length of the string ).

A1 a2 a3... An (1 <= ai <= 1000)

Output For each test case, output "Case # x: ans", in which x is the case number counted from one, 'ans 'is the points you can get at most
Sample Input
200?01 2 4 8????1 2 4 8

Sample Output
Case #1: 12Case #2: 15Hinthttps://en.wikipedia.org/wiki/Gray_codehttp://baike.baidu.com/view/358724.htm 

Author UESTC
Source 2015 Multi-University Training Contest 7
Recommend wange2014

 

Question: Give an n-bit binary number. Some digits tell us that some digits are uncertain. convert it into a gray code and get the score of the corresponding position at the position 1, the maximum number of questions.

 

Idea: the formula for Binary Gray code conversion is G [0] = B [0], G [I] = B [I] ^ B [I-1], so when the binary I-bit value and the I-1-bit value are different, G [I] = 1, you can get A [I], Otherwise G [I] = 0, the score cannot be obtained. The DP equation is dp [I] [0] = max (dp [I-1] [0], dp [I-1] [1] + a [I]), dp [I] [1] = max (dp [I-1] [1], dp [I-1] [0] + a [I]), the final result is max (dp [n-1] [1], dp [n-1] [0]).

 

Code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                # Include
               
                 # Include
                
                  # Include
                 
                   # Include
                  
                    # Include
                   
                     # Include
                    
                      # Include
                     
                       # Include
                      
                        # Include
                       
                         Using namespace std; # define PB push_back # define MP make_pair # define REP (I, x, n) for (int I = x; I <(n); ++ I) # define FOR (I, l, h) for (int I = (l); I <= (h); ++ I) # define FORD (I, h, l) for (int I = (h); I> = (l); -- I) # define SZ (X) (int) (X ). size () # define ALL (X ). begin (), (X ). end () # define RI (X) scanf (% d, & (X) # define RII (X, Y) scanf (% d, & (X ), & (Y) # define RIII (X, Y, Z) scanf (% d, & (X), & (Y), & (Z )) # define DRI (X) int (X); scanf (% d, & X) # define DRII (X, Y) int X, Y; scanf (% d, & X, & Y) # define DRIII (X, Y, Z) int X, Y, Z; scanf (% d, & X, & Y, & Z) # define OI (X) printf (% d, X); # define RS (X) scanf (% s, (X) # define MS0 (X) memset (X), 0, sizeof (X) # define MS1 (X) memset (X),-1, sizeof (X ))) # define LEN (X) strlen (X) # define F first # define S second # define Swap (a, B) (a ^ = B, B ^ =, a ^ = B) # define Dpoint strcut node {int x, y} # define cmpd int cmp (const int & a, const int & B) {return a> B ;} /* # ifdef HOME freopen(in.txt, r, stdin); # endif */const int MOD = 1e9 + 7; typedef vector
                        
                          VI; typedef vector
                         
                           VS; typedef vector
                          
                            VD; typedef long LL; typedef pair
                           
                             PII; // # define HOMEint Scan () {int res = 0, ch, flag = 0; if (ch = getchar () = '-') // determine plus and minus flag = 1; else if (ch> = '0' & ch <= '9') // obtain the complete number res = ch-'0 '; while (ch = getchar ()> = '0' & ch <= '9') res = res * 10 + ch-'0'; return flag? -Res: res ;} /* -------------- PLEASE---DO---NOT---HACK--ME ------------------ */char str [200000 + 5]; int a [200000 + 5]; int dp [200000] [2]; int main () {int T; RI (T); for (int t = 1; t <= T; t ++) {scanf (% s, str); int n = strlen (str); for (int I = 0; I
                            
                             

 

 

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.