hdu5375 Gray code
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.
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)
00?0
1 2 4 8
????
1 2 4 8
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的更多相关文章
- hdu5375 Gray code(DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5375 题目大意:给你一个二进制串,带'?'的位置能够由你来决定填'1'还是'0',补充完整之后转换成 ...
- [hdu5375 Gray code]DP
题意:给一个二进制码,其中有一些位上为'?',对每个问号确定是'0'还是'1',最后以它对应的格雷码来取数,第i位为1则取第i个数,求取得的数的和的最大值. 思路:二进制码B转换成格雷码G的方法是,G ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
随机推荐
- python模块详解 | unittest(单元测试框架)(持续更新中)
目录: why unittest? unittest的四个重要概念 加载测试用例的三个方法 自动加载测试用例 忽略测试和预期失败 生成html测试报告 why unittest? 简介: Unitte ...
- 【Problems】MySQL5.7 datetime 默认值设为‘0000-00-00 00:00:00'值出错
记录 MySQL5.7 datetime 默认值设为'0000-00-00 00:00:00'值出错 我的MySQL版本 mysql --version 5.7.28 C:\Users\x1c> ...
- scrapy框架的中间件
中间件的使用 作用:拦截所有的请求和响应 拦截请求:process_request拦截正常的请求,process_exception拦截异常的请求 篡改请求的头信息 def process_reque ...
- CSS3+JS完美实现放大镜模式
最近看到一篇讲放大镜的文章,实践后感觉效果非常好,这里分享给大家. 效果如下: 其实现核心: CSS函数,如:calc() -- 动态计算:var() -- 使用自定义变量 CSS伪元素:::befo ...
- OO第三次总结博客
规格化设计的发展历史 (因为很难寻找,所以参考了下别的同学的调研结果) 规格化设计与结构化.模块化设计密不可分,伴随着OOP语言的发展,规格化设计思想逐渐形成体系,慢慢完善. 20世纪60年代,程序的 ...
- 理解Latency和Throughput: 吞吐量和延迟
Latency,中文译作延迟.Throughput,中文译作吞吐量.它们是衡量软件系统的最常见的两个指标. 延迟一般包括单向延迟(One-way Latency)和往返延迟(Round Trip La ...
- 本地MarkDown优雅发表
本地MarkDown优雅发表 前言 身为一名程序员,记录笔记.发表博客首选便是MarkDown,现在网上有好多发表博客的地方:CSDN.博客园.简书,甚至一些大佬都有自己专属博客,但自己最喜欢的还是博 ...
- Python程序中#-*-coding: UTF-8 -*-的作用
1.通常我们在pycharm中写程序的时候会加上#-*coding: UTF-8 -*- 如: #!/usr/bin/env python3#-*-coding: UTF-8 -*-#Author x ...
- 大数据开发-Spark-拷问灵魂的5个问题
1.Spark计算依赖内存,如果目前只有10g内存,但是需要将500G的文件排序并输出,需要如何操作? ①.把磁盘上的500G数据分割为100块(chunks),每份5GB.(注意,要留一些系统空间! ...
- Java内存溢出处理
在解决java内存溢出问题之前,需要对jvm(java虚拟机)的内存管理有一定的认识. jvm管理的内存大致包括三种不同类型的内存区域:Permanent Generation space(永久保存区 ...