题目链接: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