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)