标签:
2 00?0 1 2 4 8 ???? 1 2 4 8
Case #1: 12 Case #2: 15Hinthttps://en.wikipedia.org/wiki/Gray_code http://baike.baidu.com/view/358724.htm
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<stack> #include<queue> #include<map> #define eps 1e-6 #define INF 0x3f3f3f3f using namespace std; char str[200010]; int a[200100]; int main() { int T; scanf("%d",&T); int k = 0; while(T--) { scanf("%s",str); int n = strlen(str); int p = 0; int pi = 0; for(int i=0; i<n; i++) { scanf("%d",&a[i]); } int flag = 0; int sum = 0; int minn = 99999; int cnt = 0; if(str[0] !='?') { p = str[0] - '0'; pi = 0; if(str[0] == '1') { sum += a[0]; } } else { for(int j=0; j<n; j++) { if(str[j]!='?') { p = str[j] - '0'; sum += a[j]; pi = j; minn = min(minn,a[j]); break; } else { cnt++; sum += a[j]; minn = min(minn,a[j]); } } if(cnt == n) { flag = 1; printf("Case #%d: %d\n",++k,sum); break; } if(cnt%2 == 1 && p == 1) { sum = sum - minn; } else if(cnt%2 == 0 && p == 0) { sum = sum - minn; } } int pp,pj; if(flag == 0) { for(int i=pi+1;i<n;) { if(str[i]!='?') { if(str[i-1]!='?' && str[i] != str[i-1]) { sum += a[i]; p = str[i] - '0'; pi = i; } i++; continue; } minn = 99999; cnt = 0; while(str[i] == '?') { sum += a[i]; minn = min(minn,a[i]); i++; cnt++; if(i==n) { break; } } if(i<n) { sum += a[i]; minn = min(minn,a[i]); pp = str[i] - '0'; pj = i; if(p!=pp && cnt%2 == 1) { sum -= minn; } else if(p == pp && cnt%2 == 0) { sum -= minn; } p = pp; pi = pj; } } printf("Case #%d: %d\n",++k,sum); } } return 0; }
版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。
标签:
原文地址:http://blog.csdn.net/yeguxin/article/details/47424595