Gray code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 129    Accepted Submission(s): 68

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
 
 
Hint

https://en.wikipedia.org/wiki/Gray_code

http://baike.baidu.com/view/358724.htm

 
Source
 
解题:动态规划
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int dp[maxn][],w[maxn],len;
char str[maxn];
int main() {
int kase,cs = ;
scanf("%d",&kase);
while(kase--) {
scanf("%s",str);
len = strlen(str);
for(int i = ; i < len; ++i)
scanf("%d",w + i);
memset(dp,-,sizeof dp);
if(str[] == '' || str[] == '?') dp[][] = ;
if(str[] == '' || str[] == '?') dp[][] = w[];
for(int i = ; i < len; ++i){
if(str[i] == '' || str[i] == '?'){
if(dp[i-][] != -) dp[i][] = max(dp[i][],dp[i-][]);
if(dp[i-][] != -) dp[i][] = max(dp[i][],dp[i-][] + w[i]);
}
if(str[i] == '' || str[i] == '?'){
if(dp[i-][] != -) dp[i][] = max(dp[i][],dp[i-][]);
if(dp[i-][] != -) dp[i][] = max(dp[i][],dp[i-][] + w[i]);
}
}
printf("Case #%d: %d\n",cs++,max(dp[len-][],dp[len-][]));
}
return ;
}

2015 Multi-University Training Contest 7 hdu 5375 Gray code的更多相关文章

  1. HDU 5375 Gray code (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...

  2. HDU 5375 Gray code(2015年多校联合 动态规划)

    题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...

  3. HDU 5375 Gray code 格雷码(水题)

    题意:给一个二进制数(包含3种符号:'0'  '1'  '?'  ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...

  4. HDU 5375 Gray code

    题意:给出一个二进制数,其中有些位的数字不确定,对于所有对应的格雷码,与一个序列a对应,第i位数字为1时得分a[i],求最大的得分. 解法:一个二进制数x对应的格雷码为x ^ (x >> ...

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

    Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. hdu 5375 Gray code 【 dp 】

    dp[i][j]表示第i位取j的时候取得的最大的分数 然后分s[i]是不是问号,s[i-1]是不是问号这大的四种情况讨论 #include<cstdio> #include<cstr ...

  7. HDU 5375 Gray Code 动归

    题意:给你一串不确定的二进制码,其对应的格雷码的每一位有对应的权值,问转换成的格雷码的能取到的最大权值是多少. 思路:没有思路,乱搞也AC #pragma comment(linker, " ...

  8. HDU 5375 Gray code(DP)

    题意:给一串字符串,里面可能出现0,1,?,当中问号可能为0或1,将这个二进制转换为格雷码后,格雷码的每位有一个权值,当格雷码位取1时.加上该位权值,求最大权值和为多少. 分析:比赛的时候愚了.竟然以 ...

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

    Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

随机推荐

  1. SSH框架整合截图总结(一)

    分页相关属性 --------------------------------------------------------------- 分页思路表单提交(只需传递当前页的值) ->acti ...

  2. [Office]PPT 2013如何设置图片为半透明?

    PPT里面似乎无法直接为图片设置透明度属性.下面是一种变通的办法. 1,插入一个和图片大小一致的图形(矩形):2,右键插入的矩形,然后在属性设置里选择“图片填充”,选择以需要的图片填充到该矩形里:3, ...

  3. hdu 1542 线段树之扫描线之面积并

    点击打开链接 题意:给你n个矩形,求它们的面积,反复的不反复计算 思路:用线段树的扫描线完毕.将X坐标离散化后,从下到上扫描矩形,进行各种处理,看代码凝视把 #include <stdio.h& ...

  4. POJ 3243

    Babystep算法.具体为什么,我也不太明白,好像资料不多. #include <iostream> #include <cstdio> #include <cstri ...

  5. thrift java示例

    thrift java示例 使用IntelliJ IDEA作为开发工具: 增加proto文件夹,里面写上sayHello.proto syntax = "proto3"; opti ...

  6. VC 获取控制台窗体的句柄(hWnd)

    在Windows中,句柄是一个系统内部数据结构的引用. 比如当你操作一个窗体.或说是一个Delphi窗体时,系统会给你一个该窗体的句柄,系统会通知你:你正在操作142号窗体.就此你的应用程序就能要求系 ...

  7. Spring Boot + Bootstrap + jQuery + Freemarker

    Spring Boot + Bootstrap + jQuery + Freemarker 原文地址:http://qilu.me/post/tech/2018-03-18 最近在写一些Web的东西, ...

  8. [Java]serialVersionUID的作用

    简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的.在进行反序列化时,JVM会把传来的 字节流中的serialVersionUID与本地相应实体(类 ...

  9. Fine-tuning CaffeNet for Style Recognition on “Flickr Style” Data 数据下载遇到的问题

    (下载的时候没有提示 不知道是正在下 还是出现错误 卡着了)..一直没有反应 下载前要以管理员身份运行 sudo su 再 python examples/finetune_flickr_style/ ...

  10. Spark RDD概念学习系列之如何创建RDD

    不多说,直接上干货! 创建RDD 方式一:从集合创建RDD (1)makeRDD (2)Parallelize 注意:makeRDD可以指定每个分区perferredLocations参数,而para ...