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. (二)数据源处理6-excel数据转换实战(下)

    将结果的所有数据整理如下: {'api_case_01': [{'测试用例编号': 'api_case_01', '测试用例名称': '获取access_token接口测试', '用例执行': '是' ...

  2. 诸葛 VS 庞统,拿下 Paxos 共识算法

    前言 分布式确实是一个有趣的话题,只要你留心观察,分布式在生活中无处不在. 悟空哥最开始学习分布式是从一篇非常用心写的技术征文开始的,而且这篇文章获得了征文第一名,在此感谢掘金社区提供的平台.想学习的 ...

  3. Subline Text 3 安装

    Subline Text 3 下载 下载链接 http://www.sublimetext.com/3 ,下载Subline Text3的安装包,这里以 64位的windows10为例,如果是其他操作 ...

  4. 【高级排序算法】2、归并排序法的实现-Merge Sort

    简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...

  5. 【Linux】CentOS8 初体验

    一.部署CentOS8虚拟机 1.下载Centos8镜像 下载地址: https://www.centos.org/download/ 可以选择国内的下载源,比较快,这里推荐清华的和阿里的 2.下载完 ...

  6. LeetCode349. 两个数组的交集

    题目 给定两个数组,编写一个函数来计算它们的交集. 分析 数组元素值可以很大,所以不适合直接开数组进行哈希,这里要学习另一种哈希方式:集合 集合有三种,区别见下面代码随想录的Carl大佬的表格,总结的 ...

  7. 过压保护IC和带LDO模式的Li+充电器前端保护IC

    PW2601是一种充电器前端集成电路,旨在为锂离子提供保护电池充电电路故障.该设备监测输入电压,电池电压以及充电电流,以确保所有三个参数都在正常范围内工作.这个该设备将关闭内部MOSFET断开,以保护 ...

  8. Ubuntu20.04安装Typora

    Ubuntu20.04安装Typora 安装方法 # optional, but recommended sudo apt-key adv --keyserver keyserver.ubuntu.c ...

  9. Vue之创建组件之配置路由!

    Vue之创建组件之配置路由!== 第一步: 当然就是在我们的试图文件夹[views]新建一个文件夹比如home 在home文件夹下面新建一个文件index.vue 第二步:在router目录下做如下事 ...

  10. chain issues incorrect order,EXtra certs,Contains anchor

    背景: 下载颁发下来的ssl证书安装好之后网站正常显示安全,但是通过ssl证书网站去检测报错误:chain issues incorrect order,EXtra certs,Contains an ...