题意:给一个二进制码,其中有一些位上为'?',对每个问号确定是'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. XML外部实体注入[转载]

    前言 对于xxe,深入的太少,一般做题也是复制payload再修改,没有了解过内部的结构规范等.这里转载了一篇先知社区的文章,排版了一下适合博客样式.文章总结的很好,结合了很多篇的博客文章,看完也是对 ...

  2. 手写一个简单的HashMap

    HashMap简介 HashMap是Java中一中非常常用的数据结构,也基本是面试中的"必考题".它实现了基于"K-V"形式的键值对的高效存取.JDK1.7之前 ...

  3. WGCLOUD如何监控数据库

    WGCLOUD默认是支持mysql,sqlserver,oracle,postgresql数据库监控,不用添加任何配置. DB2也是支持的,但是需要做一些小修改. 因为db2驱动包版本比较多,所以要自 ...

  4. Python最佳工程实践,建立一个完美的工程项目

    在程序开发时候一套好的开发环境和工具栈,可以帮我们极大的提高开发的效率,避免把大量时间浪费在周边琐事上.本文以Python为例,教大家如何快速打造完美的Python项目开发环境:内容涵盖了模块依赖管理 ...

  5. java Int类型转为double 类型;string转double

    int a=12; double b=(double)a; or double c=Double.valueOf((double)a); string a_s="12"; doub ...

  6. TT企业微信社群辅助,企业微信社群辅助工具,允许批量添加好友,自动同意添加请求,自动回复消息

    TT企业微信社群辅助,企业微信社群辅助工具,允许批量添加好友,自动同意添加请求,自动回复消息 界面截图 TT企业微信社群辅助工具下载 链接: https://pan.baidu.com/s/1Y2An ...

  7. shell脚本之awk(一)

     运维必备技能 概述: 1.awk是一种编程语言,用于linux/unix下对文本和数据进行扫描.处理数据来源:标准输入.文件.管道.  2.linux中常用的awk编译器版本有mawk,gawk.R ...

  8. 3.k均值的算法

    一.课堂练习 # 课堂练习 from sklearn.datasets import load_iris # 导入鸢尾花数据 iris=load_iris() iris iris.keys() dat ...

  9. python25之进制转换

    一.进制转换函数 bin():将十进制转换为二进制 oct():将十进制转换为八进制 hex():将十进制转换为十六进制 >>> x=1234>>> bin(x)' ...

  10. /proc/[pid]/status

    http://man7.org/linux/man-pages/man5/proc.5.html /proc/[pid]/status Provides much of the information ...