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

思路:如果了解格雷码的转换,相信能很快看出一些端倪。偷别人的图:

      

  分析一下:所给的二进制数要转成格雷码,只与所给二进制有关。即不需要利用已经某些转换好的格雷码字。

  接下来分析5个位的串 :

  (1)00?00  仅有1个问号,只会与后面那些连续且非问号的串转成格雷码有关

  (2)00??0  有连续的1个问号,这才需要用到dp啊,因为所有问号有很多组合可能,但是他们满足:第i位只与前1位有关。所以仅需记录此位为0的结果,和为1的结果。

  (3)0?0?0  //问号不连续,按第1种处理。

  没有问号的串没有什么DP可言,直接转。

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=; char str[N];
int a[N];
int dp[N][]; //结果只有两种,第i位为0/1 int get_sum(int pos, int cur)//选个大的前缀,但并不需要关心其究竟是多少
{
int ans1=dp[pos-][]+ (^cur)*a[pos]; //选0试试
int ans2=dp[pos-][]+ (^cur)*a[pos]; //选1试试
return max(ans1, ans2);
} int cal(int n)
{
memset(dp, , sizeof(dp));
if(str[]=='' || str[]=='?') dp[][]= a[];
for(int i=; i<n; i++)
{
int t=str[i]-''; //当前
int p=str[i-]-''; //前一位
if( str[i]=='?' && str[i-]=='?' )
{
dp[i][]=get_sum(i, ) ;
dp[i][]=get_sum(i, ) ;
}
if( str[i]=='?' && str[i-]!='?' )
{
dp[i][]= dp[i-][p] +( p^) *a[i]; //前面是固定的,没啥好选。
dp[i][]= dp[i-][p] +( p^) *a[i];
}
if( str[i]=='' || str[i]=='')
{
if(str[i-]=='?') //前面是问号,取大者即可。
dp[i][t]=max( dp[i-][]+ (^t)*a[i], dp[i-][]+ (^t)*a[i] );
else
dp[i][t]= dp[i-][p]+ (p^t)*a[i];
}
}
return max(dp[n-][], dp[n-][]);
} int main()
{
freopen("input.txt", "r", stdin);
int t, tmp, j=;
char c;
cin>>t;
while(t--)
{
scanf("%s", str);
int len=strlen(str);
for(int i=; i<len; i++) scanf("%d", &a[i]); printf("Case #%d: %d\n", ++j, cal(len) );
}
return ;
}

AC代码

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. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  3. gray code 格雷码 递归

    格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...

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

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

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

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

  6. [LeetCode] 89. Gray Code 格雷码

    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. Gray Code - 格雷码

    基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...

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

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

随机推荐

  1. C#中反射泛型 CreateInstance

    假设1我有个类叫SortClass,类中有个BubbleSort(int[] array)用于给数组进行排序. 假设2我有个类叫SortT<T>,类中有个BubbleSort(T[] ar ...

  2. 使用git了解代码编写过程

    在看教程时,有的老师会将代码放到github,如果不想跟着视频一步一步来,那就直接clone整个代码,但整个看着又有点蒙,那就使用版本切换的功能了. 首先 git clone 下载下来 git log ...

  3. POJ 1573

    #include<iostream> #include<stdio.h> #define MAXN 15 using namespace std; char _m[MAXN][ ...

  4. Peer certificate cannot be authenticated with known CA certificates.

    I was trying to post to a webservice and was getting the 60 error code: Peer certificate cannot be a ...

  5. 传说中的WCF(9):流与文件传输

    在使用Socket/TCP来传输文件,弄起来不仅会有些复杂,而且较经典的“粘包”问题有时候会让人火冒七丈.如果你不喜欢用Socket来传文件,不妨试试WCF,WCF的流模式传输还是相当强大和相当实用的 ...

  6. Windows 回调监控 <一>

    在x86的体系结构中,我们常用hook关键的系统调用来达到对系统的监控,但是对于x64的结构,因为有PatchGuard的存在,对于一些系统关键点进行hook是很不稳定的,在很大几率上会导致蓝屏的发生 ...

  7. python爬煎蛋妹子图

    # python3 # jiandan meizi tu import urllib import urllib.request as req import os import time import ...

  8. Spine的纹理导出问题

    发现美术给过来的资源,集合到unity后,发现用Spine的默认材质Spine/Skeleton有毛边问题.对比demo的图片后发现demo的图片(都是png格式)没有白色块,而自己的图片有. 原因是 ...

  9. @RequestBody 的正确使用办法

    1.以前一直以为在SpringMVC环境中,@RequestBody接收的是一个Json对象,一直在调试代码都没有成功,后来发现,其实 @RequestBody接收的是一个Json对象的字符串,而不是 ...

  10. 跨浏览器兼容的HTML5视频音频播放器

    HTML5的video和audio标签是用来在网页中加入视频和音频的标签,在支持html5的浏览器中不需要预先加载Adobe Flash浏览器插件就能轻松快速的播放视频和音频文件.而html5medi ...