HDU 5375 Gray code (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375
题面:
Gray code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 626 Accepted Submission(s): 369
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.
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)
2
00? 0
1 2 4 8
?? ??
1 2 4 8
Case #1: 12
Case #2: 15Hinthttps://en.wikipedia.org/wiki/Gray_code http://baike.baidu.com/view/358724.htm
解题:
类似2014区域赛的一道简单dp。
首先要知道格雷码是怎么算的,格雷码就是当前这一位的二进制表示和其前一位的二进制表示同样时,取0否则取1.(觉得最高位前面一位为0)。
状态转移方程为:
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i]);
dp[i][1]=max(dp[i-1][1],dp[i-1][0]+a[i]);
但由于某些位是给定的,所以仅仅能算当中部分状态。加一些if推断就可以,详见代码。如有问题。欢迎交流!!
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 1000000000
using namespace std;
char s[200005];
int a[200005],dp[200005][2];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int t,len,i,cnt=1;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%s",s+1);
len=strlen(s+1);
for(i=1;i<=len;i++)
{
scanf("%d",&a[i]);
getchar();
}
memset(dp,0,sizeof(dp));
if(s[1]=='0')
dp[1][0]=0;
else if(s[1]=='1')
dp[1][1]=a[1];
else
{
dp[1][1]=a[1];
dp[1][0]=0;
}
for(i=2;i<=len;i++)
{
if(s[i]=='0')
{
if(s[i-1]=='0')
dp[i][0]=dp[i-1][0];
else if(s[i-1]=='1')
dp[i][0]=dp[i-1][1]+a[i];
else
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i]);
}
else if(s[i]=='1')
{
if(s[i-1]=='0')
dp[i][1]=dp[i-1][0]+a[i];
else if(s[i-1]=='1')
dp[i][1]=dp[i-1][1];
else
dp[i][1]=max(dp[i-1][0]+a[i],dp[i-1][1]);
}
else
{
if(s[i-1]=='0')
{
dp[i][1]=dp[i-1][0]+a[i];
dp[i][0]=dp[i-1][0];
}
else if(s[i-1]=='1')
{
dp[i][1]=dp[i-1][1];
dp[i][0]=dp[i-1][1]+a[i];
}
else
{
dp[i][1]=max(dp[i-1][0]+a[i],dp[i-1][1]);
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i]);
}
}
}
printf("Case #%d: ",cnt++);
printf("%d\n",max(dp[len][0],dp[len][1]));
}
return 0;
}
HDU 5375 Gray code (简单dp)的更多相关文章
- HDU 5375——Gray code——————【dp||讨论】
Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu 5375 - Gray code(dp) 解题报告
Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 5375 Gray code 【 dp 】
dp[i][j]表示第i位取j的时候取得的最大的分数 然后分s[i]是不是问号,s[i-1]是不是问号这大的四种情况讨论 #include<cstdio> #include<cstr ...
- HDU 5375 Gray code(DP)
题意:给一串字符串,里面可能出现0,1,?,当中问号可能为0或1,将这个二进制转换为格雷码后,格雷码的每位有一个权值,当格雷码位取1时.加上该位权值,求最大权值和为多少. 分析:比赛的时候愚了.竟然以 ...
- 2015 Multi-University Training Contest 7 hdu 5375 Gray code
Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 5375 Gray code(2015年多校联合 动态规划)
题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...
- hdu 5375 Gray code dp
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; cons ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- HDU 5375 Gray code
题意:给出一个二进制数,其中有些位的数字不确定,对于所有对应的格雷码,与一个序列a对应,第i位数字为1时得分a[i],求最大的得分. 解法:一个二进制数x对应的格雷码为x ^ (x >> ...
随机推荐
- html+css实现选项卡功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Xshell高级后门完整分析报告
Xshell高级后门完整分析报告 from:https://security.tencent.com/index.php/blog/msg/120 1. 前言 近日,Xshell官方发布公告称其软件中 ...
- django 笔记15 ajax系列
参考 http://www.cnblogs.com/wupeiqi/articles/5703697.html # 原生操作# jQuery操作# 伪Ajax操作# XMLHttpReques 伪aj ...
- POJ1284 Primitive Roots (原根)
题目链接:http://poj.org/problem?id=1284 题目描述: 题目大意: 一个质数原根的个数 题解: 结论题 一个数n的原根的个数等于$\varphi(\varphi(n))$ ...
- MySQL的登录和退出(五)
如何使用MySQL? 如何实现MySQL的登录/退出 如何修改MySQL的提示符 如何实现MySQL的常用命令 如何规范MySQL语句 如何操作数据库 1.MYSQL常用参数及功能 mysql -V ...
- AngularJs轻松入门(四)模块化
在前面几节教程中,代码比较少,为了方便说明问题笔者將控制器代码都写在了HTML页面中,实际上这并不是什么好的编程习惯,而且可维护性差.通常的做法都是將处理业务逻辑的代码写在一个单独的JS文件中,然后在 ...
- C#共享WIFI能通过代码控制给连接的移动端分配IP么
用C#创建了一个虚拟WIFI,但是能不能通过代码来给连接上的移动端分配各自的IP.之前都是自动分配的IP.望大神们赐教 C#共享WIFI能通过代码控制给连接的移动端分配IP么 >> csh ...
- <Sicily>Tiling a Grid With Dominoes
一.题目描述 We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by on ...
- Chromium Graphics: Multithreaded Rasterization
Multithreaded Rasterization @nduca, @enne, @vangelis (and many others) Implementation status: crbug. ...
- CentOS7-1810 系统Samba配置说明
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件.SMB(Server Messages Block,信息服务块)通信协议是微软(Microsoft)和英特尔(Intel)在198 ...