Gray Code---hdu5375 (Gray code with binary code, normal DP)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5375

Test instructions is: give you a string of binary code, which may contain '? ' This can represent both 0 and 1,

Let us turn this binary string into a gray code string, after the converted string if the first bit is 1 then add the corresponding given a[i], the result of the maximum plus ans;

For example:

00?0

1 2 4 8

The binary string can be 0000 or 0010

0000 or 0011 After converting to Gray code.

0000 the sum of the values is 0;

The sum of the value of 0011 is a[3]+a[4]=12;

DP[I][J] represents the maximum value when the binary I bit is J. Also be aware of initialization.

#include <stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;#defineN 200020#defineINF 0XFFFFFFFintA[n], dp[n][2];CharS[n];intMain () {intT, N, t=1; scanf ("%d", &T);  while(t--) {memset (s),0,sizeof(s)); Memset (A,0,sizeof(a)); Memset (DP,0,sizeof(DP)); scanf ("%s", s); N=strlen (s);  for(intI=0; i<n; i++) {scanf ("%d", &A[i]); dp[i][0] = dp[i][1] = -INF; }        if(s[0]=='0'|| s[0]=='?') dp[0][0] =0; if(s[0]=='1'|| s[0]=='?') dp[0][1] = a[0];  for(intI=1; i<n; i++)        {            if(s[i]=='0'|| s[i]=='?') dp[i][0] = max (dp[i-1][0], dp[i-1][1]+A[i]); if(s[i]=='1'|| s[i]=='?') dp[i][1] = max (dp[i-1][1], dp[i-1][0]+A[i]); }        intAns=max (dp[n-1][0], dp[n-1][1]); printf ("Case #%d:%d\n", t++, ans); }    return 0;}
View Code

Gray Code---hdu5375 (Gray code with binary code, normal DP)

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.