Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 208 Accepted Submission(s): 117
php
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<string> #include<iostream> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<vector> #include<map> #include<stdlib.h> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=200000+5; int dp[MAXN][2],a[MAXN]; char str[MAXN]; int kase,len; int main() { //freopen("in.txt","r",stdin); int Case=0; scanf("%d",&kase); while(kase--) { memset(str,0,sizeof(str)); memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); scanf("%s",str); len=strlen(str); for(int i=0;i<len;i++) scanf("%d",&a[i]); dp[0][0]=0; dp[0][1]=a[0]; for(int i=1;i<len;i++) { if(str[i]=='?') { if(str[i-1]=='?') { dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i]);//假設當前位爲0,前一位爲0則轉換後仍是0,前一位爲1則轉換後爲1 dp[i][1]=max(dp[i-1][0]+a[i],dp[i-1][1]); } else { dp[i][0]=dp[i-1][str[i-1]-'0']+a[i]*(0^(str[i-1]-'0')); dp[i][1]=dp[i-1][str[i-1]-'0']+a[i]*(1^(str[i-1]-'0')); } } else { if(str[i-1]=='?') dp[i][str[i]-'0']=max(dp[i-1][0]+a[i]*(0^(str[i]-'0')),dp[i-1][1]+a[i]*(1^(str[i]-'0'))); else dp[i][str[i]-'0']=dp[i-1][str[i-1]-'0']+a[i]*((str[i-1]-'0')^(str[i]-'0'));//注意運算符的優先級 } } printf("Case #%d: ",++Case); if(str[len-1]=='?') printf("%d\n",max(dp[len-1][0],dp[len-1][1])); else printf("%d\n",dp[len-1][str[len-1]-'0']); } return 0; }