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. BeanPostProcessor bean 的后置处理器

    一. 自定 bean 的后置处理器 MyBeanPostProcessor 类.当你在初始化容器中的 bean 之前和之后,都会调用该处理器中的方法 @Component //将该后后置处理器加入到容 ...

  2. hdu 3177贪心

    #include<stdio.h>/*只能按这种形式排序单纯一种形式是不对的,按ai排序 20 2 1 1 10 20 按bi排序 20 2 5 17 1 16 都是不对的 二a.u+b. ...

  3. spring的关于数据源的datasource接口的深入理解

    1.DataSource的接口这是一个spring接口,可以获取数据库的Connection.是标准化的,取得连接的一种方式. 默认市面上有两个数据库连接池实现了spring的datasource接口 ...

  4. Grace Hopper 葛丽丝 霍普

    Grace Murray Hopper(1906-1992), COBOL之母, Debug之母, A ship in port is safe, but that is not what ships ...

  5. stylesheet_link_tag,javascript_include_tag无效?

    stylesheet_link_tag,javascript_include_tag无效? http://stackoverflow.com/questions/28241981/rails-4-ex ...

  6. TT流程随笔

    细节: 如果本地可以自动登录, 先实现本地登录,发送事件通知,再请求登录服务器 如果本地不可以登录(第一次或退出后),直接请求登录服务器 登录服务器返回消息服务器ip port / 文件服务器 链接消 ...

  7. uva 10534 Wavio Sequence LIS

    // uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...

  8. 【scikit-learn】交叉验证及其用于參数选择、模型选择、特征选择的样例

     内容概要¶ 训练集/測试集切割用于模型验证的缺点 K折交叉验证是怎样克服之前的不足 交叉验证怎样用于选择调节參数.选择模型.选择特征 改善交叉验证 1. 模型验证回想¶ 进行模型验证的一个重要目 ...

  9. linux + nginx 的配置优化

    linux 关于TCP/IP 的优化配置  配置文件/etc/sysctl.conf    修改完文件生效的命令  /sbin/sysctl -p 如下是总结的配置内容及说明 net.ipv4.con ...

  10. sql server 启用数据库的 Service Broker

    使用SqlDependency时要开启Service Broker ,那么SqlDependency是什么 https://www.cnblogs.com/zhaoyihao/p/5663258.ht ...