题意:给一个二进制码,其中有一些位上为'?',对每个问号确定是'0'还是'1',最后以它对应的格雷码来取数,第i位为1则取第i个数,求取得的数的和的最大值。

思路:二进制码B转换成格雷码G的方法是,Gi=Bi^Bi+1,Gn=Bn。所以第i位如果为'?',那么选'1'还是'0'只会影响邻位,于是用dp即可解决。

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */ const int maxn = 2e5 + ; int dp[maxn][], a[maxn];
char s[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T, cas = ;
cin >> T;
while (T --) {
scanf("%s", s);
int n = strlen(s);
for (int i = ; i < n; i ++) scanf("%d", a + i);
fillchar(dp, );
for (int i = n - ; i >= ; i --) {
if (s[i] == '0') {
if (s[i + ] == '0') dp[i][] = dp[i + ][];
if (s[i + ] == '1') dp[i][] = dp[i + ][] + a[i + ];
if (s[i + ] == '?') dp[i][] = max(dp[i + ][], dp[i + ][] + a[i + ]);
}
if (s[i] == '1') {
if (s[i + ] == '0') dp[i][] = dp[i + ][] + a[i + ];
if (s[i + ] == '1') dp[i][] = dp[i + ][];
if (s[i + ] == '?') dp[i][] = max(dp[i + ][] + a[i + ], dp[i + ][]);
}
if (s[i] == '?') {
if (s[i + ] == '0') {
dp[i][] = dp[i + ][];
dp[i][] = dp[i + ][] + a[i + ];
}
if (s[i + ] == '1') {
dp[i][] = dp[i + ][] + a[i + ];
dp[i][] = dp[i + ][];
}
if (s[i + ] == '?') {
dp[i][] = max(dp[i + ][], dp[i + ][] + a[i + ]);
dp[i][] = max(dp[i + ][] + a[i + ], dp[i + ][]);
}
}
}
int ans;
if (s[] == '0') ans = dp[][];
if (s[] == '1') ans = dp[][] + a[];
if (s[] == '?') ans = max(dp[][], dp[][] + a[]);
printf("Case #%d: %d\n", ++ cas, ans);
}
return ;
}

[hdu5375 Gray code]DP的更多相关文章

  1. hdu5375 Gray code

    Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary ...

  2. hdu5375 Gray code(DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5375 题目大意:给你一个二进制串,带'?'的位置能够由你来决定填'1'还是'0',补充完整之后转换成 ...

  3. hdu 5375 Gray code dp

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; cons ...

  4. (DP 雷格码)Gray code -- hdu -- 5375

    http://acm.hdu.edu.cn/showproblem.php?pid=5375 Gray code Time Limit: 2000/1000 MS (Java/Others)    M ...

  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. 递推DP HDOJ 5375 Gray code

    题目传送门 /* 题意:给一个串,只能是0,1,?(0/1).计算格雷码方法:当前值与前一个值异或,若为1,可以累加a[i],问最大累加值 DP:dp[i][0/1]表示当前第i位选择0/1时的最大分 ...

  7. HDU 5375 Gray code (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...

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

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

  9. URAL 1780 G - Gray Code 找规律

    G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

随机推荐

  1. Python - 和我聊Python节目最新一期介绍 - 257期:使用超级电脑,Python,射电天文学知识来探索银河系

    今天,给大家简单介绍和我聊Python的最新一期节目,第257期:使用超级电脑,Python,射电天文学知识来探索银河系. 听着标题就觉得高大上,是的,我也是这么认为的.这次请的嘉宾来头很大,来自国际 ...

  2. Navicat自动备份数据库

    @ 目录 Navicat自动备份数据库 备份与还原 修改备份位置 MySQL:5.7 Navicat:11 Windows10 重要数据库的定时备份是非常重要的,使用Navicat可以非常方便快捷地自 ...

  3. [PHP]$_SERVER参数详情

    来源:PHP中$_SERVER的详细参数与说明 $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关.$_SERVER['argv'] #传递给该脚 ...

  4. 实例讲解Springboot以Template方式整合Redis及序列化问题

    1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...

  5. shiro:加密及密码比对器(三)

    基于[自定义remle(二)]项目增加加密功能 1:数据库t_user表增加一列(盐) 增加字段:salt CREATE TABLE `t_user` ( `id` int(11) NOT NULL ...

  6. HTML+CSS教程(四)选择器(id选择器,类选择器,标签选择器,子代选择器,后代选择器,组选择器,伪类选择器)/css引入页面的形式(行内样式、内嵌样式、外联样式)

    一.回顾内容 前端的三大组成(三大模块)    HTMl(超文本标记语言) 结构层    css(层叠样式表) 表现层:用来美化HTML结构    JS(Java script)(脚本语言) 行为层: ...

  7. Java中常量的概念

    常量:在程序执行过程中,其值不发生改变的量.分类:A:字面值常量B:自定义常量字面值常量A:字符串常量(用“”括起来的内容).举例:"hello"B:整数常量 (所有的整数)举例: ...

  8. python工业互联网监控项目实战4—python opcua

    前面章节我们采用OPC作为设备到上位的信息交互的协议,本章我们介绍跨平台的OPC UA.OPC作为早期的工业通信规范,是基于COM/DCOM的技术实现的,用于设备和软件之间交换数据,最初,OPC标准仅 ...

  9. Pytorch手写线性回归

    pytorch手写线性回归 import torch import matplotlib.pyplot as plt from matplotlib.animation import FuncAnim ...

  10. all_user_func()详解

    来源:https://blog.csdn.net/moliyiran/article/details/83514495 call_user_func — 把第一个参数作为回调函数调用 通过函数的方式回 ...