码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-11-09 18:59:24      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

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
 
题意:给出一个二进制(含有‘?’的可以填‘0’ 或 ‘1’) ,当这个二进制转化为格雷码时每一位为1时都对应着一个值 a[i],问怎样填这些值能使得到的值最大? 。。。 ( 关键是当第一位二进制数是‘1’ 要直接加上的、、、)自然二进制码转换成二进制格雷码, 方法是保留二进制码的最高位作为格雷码的最高位而次高位格雷码为二进制码的高位与次高位异或。。。。。(就是次高位与最高位不相等时为1,相等为0)
 
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int N=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;

typedef long long LL;

char s[N];
int a[N];

int main ()
{
    int T, len, i, k = 0;
    int cou, Min, ans; ///cou统计每个区间?的个数,Min记录每个?区间内数组a的最小值
    char Start, End; ///Start为?区间的前一个字符,End为?区间的后一个字符

    scanf("%d", &T);

    while (T--)
    {
        scanf("%s", s);
        len = strlen(s);
        for (i = 0; i < len; i++)
            scanf("%d", &a[i]);

        k++;
        cou = 0;
        Min = INF;
        Start = End = 0; ///初始化为0,避免s[0]就是?,并且初始化为0并不影响这个二进制
        ans = 0;

        for (i = 0; i < len; i++)
        {
            if (s[i] == ?)
            {
                ans += a[i];
                cou++;
                Min = min(Min, a[i]);
            }
            else
            {
                End = s[i];

                if (cou > 0) ///当cou==0时不能进行这一步,会出现多加多减的情况
                {

                    ans += a[i]; ///?区间后的第一个字符对应的值要加上

                    if (cou % 2 == 0 && Start == End) ///特判如0????0这种情况
                        ans -= min(Min, a[i]);
                    else if (cou % 2 != 0 && Start != End) ///特判如0???1这种情况
                        ans -= min(Min, a[i]);
                }

                if (i == 0 && s[i] == 1) ans += a[i]; 
                if (i > 0 && s[i-1] != ? && s[i] != s[i-1]) ans += a[i]; ///都是数字并且不相等,可以直接加上

                Start = s[i];
                Min = INF;
                cou = 0;
            }
        }

        printf("Case #%d: %d\n", k, ans);
    }

    return 0;
}

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

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4950697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!