HDU 5375

Gray code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 627    Accepted Submission(s): 370

Problem Description

The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed to prevent spurious
output from electromechanical switches. Today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.

Now , you are given a binary number of length n including ‘0’ , ’1’ and ‘?’(? means that you can use either 0 or 1 to fill this position) and n integers(a1,a2,….,an) . A certain binary number corresponds to a gray code only. If the ith bit of this gray code
is 1,you can get the point ai.

Can you tell me how many points you can get at most?

For instance, the binary number “00?0” may be “0000” or “0010”,and the corresponding gray code are “0000” or “0011”.You can choose “0000” getting nothing or “0011” getting the point a3 and a4.

Input

The first line of the input contains the number of test cases T.

Each test case begins with string with ‘0’,’1’ and ‘?’.

The next line contains n (1<=n<=200000) integers (n is the length of the string).

a1 a2 a3 … an (1<=ai<=1000)

Output

For each test case, output “Case #x: ans”, in which x is the case number counted from one,’ans’ is the points you can get at most

Sample Input

2
00?0
1 2 4 8
????
1 2 4 8

Sample Output

Case #1: 12
Case #2: 15

Hint

https://en.wikipedia.org/wiki/Gray_code

http://baike.baidu.com/view/358724.htm

Source

2015 Multi-University Training Contest 7

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5379 5378 5377 5376 5374

第一个例子:

00?0

此时默认左边为0  即000?0。

所以可以得到0^0=0,0^0=0。?代表可以为0或者1.如果为1的话,则0^1=1,1^0=1 得到0011     就算出0*1+0*2+1*4+1*8  如果为0的话那么全为0 0000 就算出0.

题意显然是问你?填多少时,值是最大的。这个序列的长度为20W直接暴力肯定跪。

容易想到动态规划,,以dp[i][1] 代表在i这个位置为1的情况,dp[i][0]代表在i这个位置为0的情况,我们枚举每一个i的前面的为0,1,?的三种情况进行讨论。遇到?在进行两种情况的讨论,不断的更新答案,就可以了。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
const int MAXN = 210000;//点数的最大值
const int MAXM = 604000;//边数的最大值
const LL INF = 1152921504;
const LL mod= 258280327;
int num[MAXN],b[MAXN],a[MAXN];
char s[MAXN];
int dp[MAXN][2];
int main()
{
    int t,cas=1;
    cin>>t;
    while(t--)
    {
        scanf("%s",s+1);
        int n=strlen(s+1);
        memset(dp,0,sizeof(dp));
        a[0]=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&num[i]);
            if(s[i]=='?')
                a[i]=2;
            else a[i]=s[i]-'0';
        }
        for(int i=1; i<=n; i++)
        {
            if(a[i]==2)
            {
                if(a[i-1]==2)
                {
                    dp[i][1]=max(dp[i-1][0]+num[i],dp[i][1]);
                    dp[i][1]=max(dp[i-1][1],dp[i][1]);
                }
                else if(a[i-1]==0)
                    dp[i][1]=max(dp[i-1][0]+num[i],dp[i][1]);
                else
                    dp[i][1]=max(dp[i-1][1],dp[i][1]);
                if(a[i-1]==2)
                {
                    dp[i][0]=max(dp[i-1][1]+num[i],dp[i][0]);
                    dp[i][0]=max(dp[i-1][0],dp[i][0]);
                }
                else if(a[i-1]==1)
                {
                    dp[i][0]=max(dp[i-1][1]+num[i],dp[i][0]);
                }
                else
                {
                    dp[i][0]=max(dp[i-1][0],dp[i][0]);
                }
            }
            else if(a[i]==1)
            {

                if(a[i-1]==2)
                {
                    dp[i][1]=max(dp[i-1][0]+num[i],dp[i][1]);
                    dp[i][1]=max(dp[i-1][1],dp[i][1]);
                }
                else if(a[i-1]==0)
                    dp[i][1]=max(dp[i-1][0]+num[i],dp[i][1]);
                else
                    dp[i][1]=max(dp[i-1][1],dp[i][1]);
            }
            else if(a[i]==0)
            {
                if(a[i-1]==2)
                {
                    dp[i][0]=max(dp[i-1][1]+num[i],dp[i][0]);
                    dp[i][0]=max(dp[i-1][0],dp[i][0]);
                }
                else if(a[i-1]==1)
                {
                    dp[i][0]=max(dp[i-1][1]+num[i],dp[i][0]);

                }
                else
                {
                    dp[i][0]=max(dp[i-1][0],dp[i][0]);
                }
            }
        }
        printf("Case #%d: %d\n",cas++,max(dp[n][1],dp[n][0]));
    }
    return 0;
}
/*
2
??110
1 2 3 4 5
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 00:59:14

HDU 5375的相关文章

hdu 5375 Gray code(DP)

hdu 5375 Gray code Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed

HDU 5375(2015多校7)-Gray code(dp)

题目地址:HDU 5375 题意:给你一个二进制串,带'?'的位置可以由你来决定填'1'还是'0',补充完整之后转换成格雷码表示,每一个位置都有一个权值a[i],只有格雷码为'1'的位可以加上权值,问你最终权值之和最大为多少. 思路:首先要明白二进制码和格雷码是如何转换的: dp[i][0]表示第i位为0的时候的最大值,dp[i][1]表示第i位为1的时候的最大值.对于第i位的最大值由dp[i-1][0],dp[i-1][1]和当前权值a[i]得到.当前的位的二进制码有0,1,?三种情况,所以就

HDU 5375 Gray code (简单dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 626    Accepted Submission(s): 369 Problem Description The reflected binary cod

hdu 5375 多校

Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 604    Accepted Submission(s): 357 Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary

HDU 5375(Gray code-格雷码dp)

Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 860    Accepted Submission(s): 490 Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary

HDU 5375 Gray code

题意:给出一个二进制数,其中有些位的数字不确定,对于所有对应的格雷码,与一个序列a对应,第i位数字为1时得分a[i],求最大的得分. 解法:一个二进制数x对应的格雷码为x ^ (x >> 1),题解说是个dp……但其实就四种情况……判一下就好了 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h>

HDU HDU 5375 Gray code(二进制和格雷码)

Description: The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed to prevent spurious outpu

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

Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 684    Accepted Submission(s): 402 Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary

hdu 5375 Gray code dp

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N=200000+5; const int inf=1<<24; int dp[N][2],a[N]; char s[2*N]; int main() { int n,m,i,_; scanf("%d",&_); for(int k=1;k<=_;k+

HDU 5375 DP

模拟格雷码,给出格雷码每一位为1可得到的分数,然后给出一串二进制,之中'?'可为0或者1,求转换成格雷码的最大分数 二进制码→格雷码(编码): 此方法从对应的n位二进制码字中直接得到n位格雷码码字,步骤如下: 对n位二进制的码字,从右到左,以0到n-1编号 如果二进制码字的第i位和i+1位相同,则对应的格雷码的第i位为0,否则为1(当i+1=n时,二进制码字的第n位被认为是0,即第n-1位不变) 然后DP求解每一位分别为1或者0的最大分数即可 #include "stdio.h" #i