hdu5375 Gray Code

Source: Internet
Author: User

Problem DescriptionThe reflected binary code, also known as gray code after Frank Gray, is a binary numeral system where t Wo successive values 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.

Inputthe 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)

Outputfor each test case, output "case #x: ans", in which X are the case number counted from one, ' ans ' are the points you ca n Get at most
Sample Input
200?01 2 4 8???? 1 2 4 8

Sample Output
Case #1:12

Case #2:15

The operation of Gray Code is i^ (i>>1), give you each bit corresponding value and this number (including 0,1,? ),? can be 0, or can be 1, to find the maximum value, is a DP problem.

DP[I][1] Indicates the maximum value that can be generated when I is 1, anddp[i][0] represents the maximum value that can be generated by the first.

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < math.h> #include <vector> #include <map> #include <set> #include <queue> #include <stack > #include <string> #include <algorithm>using namespace std, #define INF 88888888#define MAXN 200050char s[ Maxn];int Dp[maxn][2],a[maxn];int Main () {int n,m,i,j,t,len,num1=0;scanf ("%d", &t), while (t--) {scanf ("%s", s+1); Len=strlen (s+1); for (i=1;i<=len;i++) {scanf ("%d", &a[i]);} Dp[1][0]=dp[1][1]=-inf;if (s[1]== ' 1 ') {dp[1][1]=a[1];} if (s[1]== ' 0 ') {dp[1][0]=0;} if (s[1]== '? ') {dp[1][0]=0;dp[1][1]=a[1];} for (i=2;i<=len;i++) {dp[i][0]=dp[i][1]=-inf;if (s[i]== ' 1 ' | | s[i]== '? ') {Dp[i][1]=max (dp[i-1][0]+a[i],dp[i-1][1]);} if (s[i]== ' 0 ' | | | s[i]== '? ') {Dp[i][0]=max (dp[i-1][0],dp[i-1][1]+a[i]);}} num1++;p rintf ("Case #%d:%d\n", Num1,max (Dp[len][0],dp[len][1]));} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu5375 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.