HDU5375——DP——Gray code

/************************************************/*格雷值与二进制转化公式,G[i] = B[i]^B[i-1],二进制前面补0dp[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;
}

  

时间: 2024-11-05 15:49:27

HDU5375——DP——Gray code的相关文章

hdu 5375 Gray code(DP)

hdu 5375 Gray code Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed

HDOJ 5375 Gray code DP

标准格雷码的性质:二进制a1 a2 ... an,对应的格雷码为a1 (a1 xor a2) ... (an-1 xor an) 题目就可以转为O(n)的dp dp[i][j]表示二进制第i位为j时前i位对应最大分数. Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 298    Accepted Submissio

hdu5375 Gray code(动态规划)

题目: Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 860    Accepted Submission(s): 490 Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a bi

hdu 5375 - Gray code(dp) 解题报告

Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 684    Accepted Submission(s): 402 Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary

HDU 5375 Gray code (简单dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 626    Accepted Submission(s): 369 Problem Description The reflected binary cod

HDU 5375——Gray code——————【dp||讨论】

Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 569    Accepted Submission(s): 337 Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary

hdu5375 Gray code

The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed to prevent spurious output from electr

leetcode笔记:Gray Code(2016腾讯软件开发笔试题)

一.题目描述 The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin wit

LeetCode:Gray Code

1.题目名称 Gray Code(格雷码) 2.题目地址 https://leetcode.com/problems/gray-code/ 3.题目内容 英文: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the