HDU HDU 5375 Gray code (binary and Gray code)

Source: Internet
Author: User

Description:

The reflected binary code, also known as gray code after Frank Gray, is a binary numeral system where the successive value s differ in only onebit (binary digit). The reflected binary code is originally designed to prevent spurious output from electromechanical switches. Today, Gray codes is widely used to facilitate error correction in digital communications such as digital terrestrial tel Evision and some cable TV systems.



Now, is given a binary number of length n including ' 0 ', ' 1 ' and '? ' (? means that can use the either 0 or 1 to the 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 was 1,you can get the point AI.
Can you tell me how many points can get to most?

For instance, the binary number "00?0" could be "0000" or "0010", and the corresponding gray code is "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 is begins with a 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 the most

Sample Input:

200?01 2 4 8???? 1 2 4 8

Sample Output:

Case #1:12Case #2:15Test Instructions:give a binary (containing '? ' Can fill ' 0 ' or ' 1 '), when this binary conversion to Gray code, each bit is 1 corresponds to a value a[i], ask how to fill these values can make the maximum value? (The key is when the first binary number is ' 1 ' to be directly added to the 、、、) natural binary code to convert to binary gray code, the method is to keep the highest bit of binary code as the top of the gray code and the second high-level gray code for the binary code of the high and the sub-high xor .... (That is, the second and the highest are not equal to 1, equal to 0)
#include <stdio.h>#include<string.h>#include<queue>#include<math.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intn=1e6+Ten;Const intinf=0x3f3f3f3f;Const intmod=1e9+7; typedefLong LongLL;CharS[n];intA[n];intMain () {intT, Len, I, k =0; intCou, Min, ans;///cou count each interval? The number, min record each? Minimum value of array A in interval    CharStart, End;///start for? The first character of the interval, end is? The next character of the intervalscanf ("%d", &T);  while(t--) {scanf ("%s", s); Len=strlen (s);  for(i =0; i < Len; i++) scanf ("%d", &A[i]); K++; Cou=0; Min=INF; Start= End ='0';///Initialize to 0, avoid s[0] yes? and initialized to 0 does not affect this binaryAns =0;  for(i =0; i < Len; i++)        {            if(S[i] = ='?') {ans+=A[i]; Cou++; Min=min (min, a[i]); }            Else{End=S[i]; if(Cou >0)///when the cou==0 is not able to take this step, there will be more and more reduction in the situation{ans+ = A[i];///? The value corresponding to the first character after the interval is added                    if(Cou%2==0&& Start = = End)///A special sentence such as 0???? 0 this situationAns-=min (min, a[i]); Else if(Cou%2!=0&& Start! = End)///A special sentence such as 0??? 1 This situationAns-=min (min, a[i]); }                if(i = =0&& S[i] = ='1') ans + =A[i]; if(I >0&& s[i-1] !='?'&& s[i]! = s[i-1]) ans + = a[i];///are numbers and are not equal, can be directly addedStart=S[i]; Min=INF; Cou=0; }} printf ("Case #%d:%d\n", k, ans); }    return 0;}

HDU HDU 5375 Gray code (binary and Gray code)

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.