题意:给一个二进制码,其中有一些位上为'?',对每个问号确定是'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. 运行一个nodejs服务,先发布为deployment,然后创建service,让集群外可以访问

    问题来源 海口-老男人 17:42:43 就是我要运行一个nodejs服务,先发布为deployment,然后创建service,让集群外可以访问 旧报纸 17:43:35 也就是 你的需求为 一个a ...

  2. C++基础 学习笔记五:重载之运算符重载

    C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...

  3. 不借助多余参数也可交换两个参数(c++,swap函数)

    利用a^a=0异或属性 [示例代码] #include<stdio.h> void data_swap(int &a,int &b){ a = a ^ b; b = a ^ ...

  4. 从零开始学AB测试:躲坑篇

    AB测试的原理很简单,只用到了最简单的统计假设检验,但表面的简单通常都隐藏着陷阱,这一点没有经过实践的摸爬滚打是不容易看到的,今天我就把前人已经踩过的坑,一共15个,给大家分享一下.在分享之前,大家脑 ...

  5. Springboot:员工管理之删除员工及退出登录(十(9))

    springboot2.2.6 delete请求报错,降至2.1.11功能可用 原因未知 构建员工删除请求 com\springboot\controller\EmployeeController.j ...

  6. python 工具链 虚拟环境和包管理工具 pipenv

    Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, ...

  7. 用Java代码来校验QQ号

    校验qq号码: 1.要求必须是5-15位数字 2.0不能开头 分析: A:键盘录入一个qq号码 B:写一个功能实现校验 C:调用功能,输出结果. 代码实现:public class RegexDemo ...

  8. 高质量动漫实时画质增强器Anime4K在mpv上的配置

    Anime4K地址 https://github.com/bloc97/Anime4K mpv地址  https://mpv.io/   这个要错峰下载,网速不太好 在C盘用户\..\AppData\ ...

  9. tp5--开启与关闭调试模式

    https://www.cnblogs.com/finalanddistance/p/8906000.html TP5 显示错误信息   在TP5中,我们运行的代码有错误无法执行时,只显示页面错误,而 ...

  10. 计算3的n次幂htm代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...