/************************************************
/* Gray value and binary conversion formula, G[i] = b[i]^b[i-1], binary front complement 0
DP[I][J] is defined as the current I-gray value, the value is J and
*/* Author:p owatr* Created time:2015-8-11 13:19:46* File name:1007.cpp ************************************ /#include <cstdio> #include <algorithm> #include <iostream> #include <sstream># Include <cstring> #include <cmath> #include <string> #include <vector> #include <queue># Include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << # Define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int MAXN = 2e6 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;char s[maxn];int Ss[max N][2];int dp[maxn][2];int A[maxn];int Main () {int T; scanf ("%d", &t); for (int cas = 1; CAs <= T; cas++) {memset (A, 0, sizeof (a)); memset (s, 0, sizeof (s)); memset (DP, 0, sizeof (DP)); scanf ("%s", s+1); int len = sTrlen (s+1); for (int i = 1; I <= len; i++) scanf ("%d", &a[i]); if (s[1] = = ' 1 ') dp[1][1] = a[1]; else if (s[1] = = ' 0 ') dp[1][0] = 0; else if (s[1] = = '? ') {Dp[1][1] = a[1], dp[1][0] = 0; } for (int i = 2; I <= len; i++) {if (s[i] = = ' 1 ') {if (s[i-1] = = ' 0 ') DP[I][1] = dp[i-1][0] + a[i]; else if (s[i-1] = = ' 1 ') dp[i][1] = dp[i-1][1]; else if (s[i-1] = = '? ') {Dp[i][1] = max (dp[i-1][1], dp[i-1][0] + a[i]); }} else if (s[i] = = ' 0 ') {if (s[i-1] = = ' 1 ') dp[i][0] = dp[i-1][1] + A[i]; else if (s[i-1] = = ' 0 ') dp[i][0] = dp[i-1][0]; else if (s[i-1] = = '? ') {Dp[i][0] = max (dp[i-1][1] + a[i], dp[i-1][0]); }} else if (s[i] = = '? ') {if (s[i-1] = = ' 1 ') {dp[i][0] = dp[i-1][1] + a[i]; DP[I][1] = dp[i-1][1]; } else if (s[i-1] = = ' 0 ') {dp[i][0] = dp[i-1][0]; DP[I][1] = dp[i-1][0] + a[i]; } else if (s[i-1] = = '? ') {Dp[i][0] = max (dp[i-1][0], dp[i-1][1]+a[i]); DP[I][1] = max (dp[i-1][1], dp[i-1][0]+a[i]); }}} printf ("Case #%d:%d\n", CAS, Max (dp[len][1], dp[len][0])); } return 0;}
Hdu5375--dp--gray Code