Gray code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 261    Accepted Submission(s): 150


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
 
题意:给你一个二进制串0、1、?  ‘?’ 可以取0和1  每一位对应一个价值a[i], 求使得对应的格雷码价值和最大是多少?
思路:dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + a[i]);
     dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + a[i]);
     dp[i][j] 表示第i位取j时的最大和。
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<limits.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<complex>
 7 #include<cstring>
 8 #include<iomanip>
 9 #include<stdio.h>
10 #include<bitset>
11 #include<cctype>
12 #include<math.h>
13 #include<string>
14 #include<time.h>
15 #include<vector>
16 #include<cmath>
17 #include<queue>
18 #include<stack>
19 #include<list>
20 #include<map>
21 #include<set>
22 
23 #define LL long long
24 
25 using namespace std;
26 const LL mod = 1e9 + 7;
27 const double PI = acos(-1.0);
28 const double E = exp(1.0);
29 const int M = 1e5 + 5;
30 
31 string s;
32 int a[M << 1];
33 int dp[M << 1][2];
34 
35 int main()
36 {
37     int t;
38     cin >> t;
39     int cas = 1;
40     while( t-- ){
41         cin >> s;
42         int n = s.size();
43         s = '0' + s;
44         for(int i = 1; i <= n; ++i)
45             scanf("%d", &a[i]);
46         memset(dp, 0, sizeof(dp));
47         for(int i = 1; i <= n; ++i)
48             dp[i][0] = dp[i][1] = -1e9;
49         dp[0][0] = 0;
50         dp[0][1] = -1e9;
51         for(int i = 1; i <= n; ++i){
52             if(s[i] != '?'){
53                 if(s[i] == '1')
54                     dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + a[i]);
55                 else
56                     dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + a[i]);
57             }
58             else if(s[i] == '?'){
59                 dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + a[i]);
60                 dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + a[i]);
61             }
62         }
63        int ans = max(dp[n][0], dp[n][1]);
64        printf("Case #%d: %d\n", cas++, ans);
65     }
66     return 0;
67 }

 

posted on 2015-08-11 21:39  Unico  阅读(159)  评论(0)    收藏  举报