HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含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 格雷码(水题)的更多相关文章
- HDU 5375 Gray code (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- gray code 格雷码 递归
格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...
- 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 ...
- [LeetCode] 89. Gray Code 格雷码
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 ...
- Gray Code - 格雷码
基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...
- HDU 5375 Gray code(2015年多校联合 动态规划)
题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...
随机推荐
- Find the smallest number whose digits multiply to a given number n
Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ...
- 十大技巧优化Android App性能
无论锤子还是茄子手机的不断冒出,Android系统的手机市场占有率目前来说还是最大的,因此基于Android开发的App数量也是很庞大的. 那么,如何能开发出更高性能的Android App?相信是软 ...
- C Primer Plus之文件输入/输出
文件 一个文件通常就是磁盘上的一段命名的存储区.但对于操作系统来说,文件就会更复杂一些.例如,一个大文件可以存储在一些分散的区段中,或者还会包含一些使操作系统可以确定其文件类型的附加数据. C将文件看 ...
- Junit单元测试学习笔记二
我们使用Eclipse自动生成了一个测试框架,在这篇文章中,我们来仔细分析一下这个测试框架中的每一个细节,知其然更要知其所以然,才能更加熟练地应用JUnit4. 一. 包含必要地Package ...
- Arraylist和Vector的区别与HashMap和Hashtable的区别
1.ArrayList和HashMap都是线程异步的,所以它们的特点是效率高,但是安全性低: 2.Vector和Hashtable都是线程同步的,所以它们的特点是效率低,但是安全性高.
- Spring框架学习之第1节
spring快速入门 ① spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...
- iOS开发--提交github代码
将的SampleTable例子提交到github 具体步骤如下: a. 首先登陆github b. 创建新的reponsitory name, description c. 打开terminal, c ...
- Redis的Order Set操作
有序集合 zadd key score1 value1 score2 value2 .. 添加元素 127.0.0.1:6379> zadd class 12 lily 13 lucy 18 l ...
- Redis的List操作
lpush key value 作用: 把值插入到链接头部 127.0.0.1:6379> lpush character a (integer) 1 127.0.0.1:6379> rp ...
- 293. Flip Game
题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...