标签:

输入
输出
样例输入
样例输出
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;
typedef double de;
const LL oo = 0x3f3f3f3f;
const LL maxn = 1e6+7;
const LL mod = 1e9+7;
char str[maxn];
LL as[maxn];
int main()
{
LL i, cas=1, T, ans, len, mark, s1, s2;
char start, en;
scanf("%lld", &T);
while(T--)
{
scanf("%s", str+1);
len = strlen(str+1);
for(i = 1; i <= len; i++)
scanf("%lld", &as[i]);
ans = mark = 0; start = ‘0‘;
///start ‘?’的 起始位置 en ‘?’终止为止 mark ‘?’的个数
for(i = 1; i <= len; i++)
{
if(str[i] == ‘?‘)
{
mark ++;
ans += as[i];
if(mark == 1) s1 = as[i];///s1区间最小值
else s1 = min(s1, as[i]);
}
else
{
s2 = as[i];///当前位置对应值
en = str[i];///记录字符类型
if(mark > 0)
{
ans += s2; ///如果有‘?’ 需要将‘?’右面第一位的值加上
if(mark%2 == 1 && start != en)///‘?’有奇数个并且两端的字符类型不同需要减去那个最小的
ans -= min(s1, s2);
if(mark%2==0 && start == en)///‘?’有偶数个并且两端的字符类型相同需要减去那个最小的
ans -= min(s1, s2);
}
mark = 0;
start = str[i];///更新初始位置
if(i == 1 && str[i] == ‘1‘) ans += as[i];///第一位是‘1’要直接加上
if( i > 1 && str[i-1] != ‘?‘ && str[i] != str[i-1])///可以转换为格雷码对应的‘1’ 加上这个值
ans += as[i];
}
}
printf("Case #%lld: %lld\n", cas++, ans);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/PersistFaith/p/4947706.html