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. 使用 C# 9 的records作为强类型ID - 初次使用

    强类型ID 实体通常是整数,GUID或者string类型,因为数据库直接支持这些类型,但是,如果实体的ID的类型是一样的,比如都是整数的ID,这有可能会出现ID值传错的问题,看下边的示例. publi ...

  2. JPEG解码--(1)JPEG文件格式概览

    由于懒和人的忘性,以前做的一些笔记再回过头看时又有些生疏了,我决定把一些内容整理出来,以供有需要的来参考. 了解的人知道其价值所在,不知道的人就弃之如废物吧. 本篇是JPEG解码系列的第一篇--JPE ...

  3. 使用Jenkins+Blue Ocean 持构建自动化部署之安卓源码打包、测试、邮件通知

    什么是BlueOcean? BlueOcean重新考虑了Jenkins的用户体验.BlueOcean由Jenkins Pipeline设计,但仍然兼容自由式工作,减少了团队成员的混乱,增加了清晰度. ...

  4. Java JDK8下载 (jdk-8u251-windows-x64和jdk-8u271-linux-x64.tar)

    jdk-8u251-windows-x64 和 jdk-8u271-linux-x64.tar 链接:https://pan.baidu.com/s/1gci6aSIFhEhjY8F48qH39Q 提 ...

  5. 攻防世界 - Web(一)

    baby_web: 1.根据题目提示,初始页面即为index,将1.php改为index.php,发现依然跳转成1.php,尝试修改抓包,出现如下回显, 2.在header中获取flag, flag: ...

  6. Netty学习:EventLoop事件机制

    目录 EventLoop是什么 EventLoop适用的场景 Netty中的EventLoop Netty中的大量inEventLoop判断 Netty是如何建立连接并监听端口的-NIOSocketC ...

  7. 基于.net5 wtm框架、uni-app微信公众号开发一、公众号授权

    前端公众号授权 公众号设置 0.首先用IIS创建一个空目录的网站用于公众号域名验证,接着把该网站内网穿透出去,推荐用utools工具,官网:https://u.tools/ 下载安装好后搜索内网穿透并 ...

  8. 与图论的邂逅06:dfs找环

    当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...

  9. win32 sdk 环境下创建状态栏

    今天在学习状态栏,出了好多的问题,这里记录下. 要创建状态栏用:CreateStatusWindow CreateStatusWindow函数创建一个状态窗口,通常用于显示应用程序的状态.窗口通常显示 ...

  10. windows10复制粘贴键突然失效无法复制粘贴的最简单办法

    报了学习班,打开了VCE的加密文档 今天复制粘贴键突然失效 在网上捣鼓了好多方法都不行最后发现看看你有没有在用加密文件,也就是网课类的文档和视频.有就把它关了关了就好了