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 to prevent spurious output from electromechanical
switches. Today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.




Now , you are given a binary number of length n including ‘0’ , ’1’ and ‘?’(? means that you can use either 0 or 1 to fill this position) and n integers(a1,a2,….,an) . A certain binary number corresponds to a gray code only. If the ith bit of this gray code
is 1,you can get the point ai.

Can you tell me how many points you can get at most?

For instance, the binary number “00?0” may be “0000” or “0010”,and the corresponding gray code are “0000” or “0011”.You can choose “0000” getting nothing or “0011” getting the point a3 and a4.
 

Input
The first line of the input contains the number of test cases T.

Each test case begins with string with ‘0’,’1’ and ‘?’.

The next line contains n (1<=n<=200000) integers (n is the length of the string).

a1 a2 a3 … an (1<=ai<=1000)
 

Output
For each test case, output “Case #x: ans”, in which x is the case number counted from one,’ans’ is the points you can get at most
 

Sample Input

2
00?0
1 2 4 8
????
1 2 4 8
 

Sample Output

Case #1: 12

Case #2: 15

Gray码的运算是i^(i>>1),给你每个位所对应的价值以及这个数(含0,1,?),?可以为0,也可以为1,求最大的价值,是一道dp题。

dp[i][1]表示第i为1时能产生的最大价值,dp[i][0]表示第i为时能产生的最大价值。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define inf 88888888
#define maxn 200050
char s[maxn];
int dp[maxn][2],a[maxn];
int main()
{
int n,m,i,j,T,len,num1=0;
scanf("%d",&T);
while(T--)
{
scanf("%s",s+1);
len=strlen(s+1);
for(i=1;i<=len;i++){
scanf("%d",&a[i]);
}
dp[1][0]=dp[1][1]=-inf;
if(s[1]=='1'){
dp[1][1]=a[1];
}
if(s[1]=='0'){
dp[1][0]=0;
}
if(s[1]=='?'){
dp[1][0]=0;dp[1][1]=a[1];
} for(i=2;i<=len;i++){
dp[i][0]=dp[i][1]=-inf;
if(s[i]=='1' || s[i]=='?'){
dp[i][1]=max(dp[i-1][0]+a[i],dp[i-1][1]);
}
if(s[i]=='0' || s[i]=='?'){
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i]);
}
}
num1++;
printf("Case #%d: %d\n",num1,max(dp[len][0],dp[len][1]));
}
return 0;
}

hdu5375 Gray code的更多相关文章

  1. hdu5375 Gray code(DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5375 题目大意:给你一个二进制串,带'?'的位置能够由你来决定填'1'还是'0',补充完整之后转换成 ...

  2. [hdu5375 Gray code]DP

    题意:给一个二进制码,其中有一些位上为'?',对每个问号确定是'0'还是'1',最后以它对应的格雷码来取数,第i位为1则取第i个数,求取得的数的和的最大值. 思路:二进制码B转换成格雷码G的方法是,G ...

  3. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  4. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  5. Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  6. 【leetcode】Gray Code (middle)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  7. [LintCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  8. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  9. LeetCode——Gray Code

    Description: The gray code is a binary numeral system where two successive values differ in only one ...

随机推荐

  1. [从源码学设计]蚂蚁金服SOFARegistry之续约和驱逐

    [从源码学设计]蚂蚁金服SOFARegistry之续约和驱逐 目录 [从源码学设计]蚂蚁金服SOFARegistry之续约和驱逐 0x00 摘要 0x01 业务范畴 1.1 失效剔除 1.2 服务续约 ...

  2. Java 在pom.xml中配置build resources, 来防止我们资源导出失败问题(Maven项目)

    在pom.xml中配置build, 来防止我们资源导出失败问题 <!--在build中配置resources, 来防止我们资源导出失败问题--> <build> <res ...

  3. /usr/bin/ld: cannot find -lc

    yum install glibc-static [root@test chkrootkit-0.50]# make sensecc -static -o strings-static strings ...

  4. 学习rac管理

    文章转自:http://blog.itpub.net/7728585/viewspace-752185/ crsctl query crs activeversion 查看版本 ocrconfig - ...

  5. 深入理解Redis之简单动态字符串

    目录 SDS SDS与C字符串的区别 SDS获取字符串长度复杂度为O(1),C字符串为O(N) SDS杜绝了缓存区溢出 减少修改字符串时带来的内存重分配次数 二进制安全 Redis没有直接使用C语言传 ...

  6. Android事件分发机制三:事件分发工作流程

    前言 很高兴遇见你~ 本文是事件分发系列的第三篇. 在前两篇文章中,Android事件分发机制一:事件是如何到达activity的? 分析了事件分发的真正起点:viewRootImpl,Activit ...

  7. 试玩 GOWOG ,初探 OpenAI(使用 NeuroEvolution 神经进化)与 Golang 多人在线游戏开发

    GOWOG: 原项目:https://github.com/giongto35/gowog 我调整过的:https://github.com/Kirk-Wang/gowog GOWOG 是一款迷你的, ...

  8. 浅谈自动化构建之grunt

    自动化构建 开发行业的自动化构建 一句话把源代码转化为生产代码,作用是脱离运行环境兼容带来的问题开发阶段使用提高效率的语法,规范 和标准,构建转换那些不被支持的特性转化成能够执行的代码. 一.简单的自 ...

  9. 网络编程 — Linux TCP服务端和客户端

    1. 服务端 #include <stdlib.h> #include <string.h> #include <errno.h> #include <sig ...

  10. Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 开源了

    开源的来了,懒人程序员的福音,.NET 生态闭环太缺开源精神了, 拥抱开源! 前言: 本人不是不喜欢现有ORM的轮子,而是发现现有的ORM 的都不太符合开发人员的一些习惯.现有的ORM 要么功能太冗余 ...