HDU5375——DP——Gray code
格雷值与二进制转化公式,G[i] = B[i]^B[i-1],二进制前面补0
dp[i][j]定义为当前是第i个格雷值,值是j的和
* Author :powatr * 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 << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 2e6 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; char s[MAXN]; int ss[MAXN][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; }