HDU 5375-gray Code (DP) Problem Solving report

Source: Internet
Author: User

Gray CodeTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 684 Accepted Submission (s): 402


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:12Case #2:Hinthttps://en.wikipedia.org/wiki/Gray_codehttp://baike.baidu.com/view/358724.htm

Test instructions: give you a string of binary code,? can either represent 1 or represent 0. Convert it to gray code, each bit has a corresponding value, ask the gray Code of all 1 to add the value, the maximum value.

Key: First we need to know how to convert binary code into gray code. Move the number of conversions you want to the right one again and the original number will be different. Here we can find the value of the current position is related to the next one, so we would like to use DP to solve this problem.

Reference code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define LL Long Long#define Max (A, B) a>b?a:busing namespace std;const int N = 200005;const int INF = 999999999;char S[n];int dp[n][2],    Num[n];int Main () {int T, case = 1;    scanf ("%d", &t);        while (t--) {GetChar ();            Gets (s);        int len = strlen (s);           for (int i = 0; i < len; i++) {scanf ("%d", &num[i]);                  } if (s[0] = = ' 1 ') {dp[0][1] = num[0];        Dp[0][0] =-inf;            } else if (s[0] = = ' 0 ') {dp[0][1] =-inf;        Dp[0][0] = 0;            } else {dp[0][1] = num[0];        Dp[0][0] = 0; } for (int i = 1; i < Len; i++) {if (s[i] = = ' 0 ') {dp[i][0] = max (dp[i-1][1] + num                [i], dp[i-1][0]);                DP[I][1] =-inf; } else if (s[i] = = ' 1 ') {dp[i][1] = max (dp[i-1][0] + num[i], dp[i -1][1]);            Dp[i][0] =-inf;                } else {dp[i][1] = max (dp[i-1][0] + num[i], dp[i-1][1]);            Dp[i][0] = max (dp[i-1][1] + num[i], dp[i-1][0]);           }} printf ("Case #%d:", case++);    printf ("%d\n", Max (Dp[len-1][1], dp[len-1][0])); } return 0;}


Copyright NOTICE: This article for Bo Master original article, casually reproduced.

HDU 5375-gray Code (DP) Problem Solving report

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.