hdoj 5375 Gray Code

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375

编码规则:tmp = XOR(gr[i],gr[i-1]);

算是找规律的题目吧,考虑?前后字符和?数目的奇偶性就可以了,一个小trick就是当碰到需要减的时候是减问号区间内最小的那个,

然后就是调试的问题:

字符串从数组第二位开始存放,scanf("%s",s+1);

当用到min和max的时候记得每次使用前要清空。

 1 #include<stdio.h>
 2 const int MAXN = 200010;
 3 int XOR( char a, char b){
 4     int aa, bb;
 5     aa = a - ‘0‘;
 6     bb = b - ‘0‘;
 7     return aa^bb;
 8 }
 9 int main(){
10     char gr[MAXN];
11     int num[MAXN];
12     int _min;           int T, N;
13     int tmp;            int cnt, sum;
14     int lft, rgt;       int TT = 0;
15     scanf("%d",&T);
16     while(T--){
17         scanf("%s",gr+1);
18         gr[0] = ‘0‘;
19         cnt = 0; sum = 0; N = 0;_min = 1000000;
20         while( gr[N] != ‘\0‘)
21             N++;
22         for(int i = 1; i < N; ++i){
23             scanf("%d",&num[i]);
24         }
25         gr[N]=‘0‘;
26         num[N] = 0;
27         N++;
28         for(int i = 1; i < N; ++i){
29             if( gr[i] != ‘?‘){
30                 tmp = XOR(gr[i],gr[i-1]);
31                 if(tmp){
32                     sum += num[i];
33                 }
34             }
35             else{
36                 cnt = 0;
37                 lft = gr[i-1];
38                 for(;i<N;++i){
39                     if(gr[i] != ‘?‘){
40                         if( num[i] < _min )
41                             _min = num[i];
42                         sum += num[i];
43                         break;
44                     }
45                     else{
46                         cnt++;
47                         sum += num[i];
48                         if( num[i] < _min )
49                             _min = num[i];
50                     }
51                 }
52                 rgt = gr[i];
53                 if(( ( lft != rgt) && ( cnt % 2 == 1))||( (lft == rgt) && (cnt % 2 == 0) )){
54                     sum = sum - _min;
55                 }
56                 _min = 1000000;
57             }
58         }
59         printf("Case #%d: %d\n",++TT,sum);
60     }
61 }
时间: 2024-10-04 21:58:54

hdoj 5375 Gray Code的相关文章

HDOJ 5375 Gray code DP

标准格雷码的性质:二进制a1 a2 ... an,对应的格雷码为a1 (a1 xor a2) ... (an-1 xor an) 题目就可以转为O(n)的dp dp[i][j]表示二进制第i位为j时前i位对应最大分数. Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 298    Accepted Submissio

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 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 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||讨论】

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

HDU 5375 Gray code(模拟)

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

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 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+