1107 - Niro loves Chinese

DESCRIPTION

Once upon a time a little girl named Niro found an ancient book in a forest. Written on it was an ancient story named Nyu Wa Repairs the Sky. This story only consisted of characters ‘0’s and ‘1’s. Like the sky, this book was broken and some of the characters was replaced by ‘?’s. Niro wanted to repair it by replacing the ‘?’s with ‘0’s and ‘1’s. However, Using a ‘0’ cost C0
yuan and using a ‘1’ cost C1 yuan. Additionally, there shouldn’t be N0 consecutive ‘0’s or N1

consecutive ‘1’s. What was the minimum cost to repair this book?

INPUT
The first line contains a positive integer T
, the number of test cases. Each test case contains two lines. In each test case, the first line contains five positive integers N, C0, C1, N0, N1.The second line contains N
characters. Each character can only be ‘0’, ‘1’ or ‘?’.
It is guaranteed that there is at least one valid way to repair the book.
OUTPUT
T
line(s). Each line contains one positive integer, the minimum cost to repair the book for the corresponding test case.
SAMPLE INPUT
2
10 2 3 4 5
1000?0001?
9 1 100 3 3
0?0?0?0?0
SAMPLE OUTPUT
5
400
对于‘?’扫描它的左右,尽量使用便宜的。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char s[100005];
int n,c0,c1,n0,n1;

struct node {
    char num;
    int sum;
}; 

struct tg{
    int co;
    int li;
}; 

struct tg ex[2];

int main(){   
    int t;scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d%d",&n,&ex[0].co,&ex[1].co,&ex[0].li,&ex[1].li);
        scanf("%s",s);
        int mini = ex[0].co < ex[1].co? 0:1;
        int maxi = ex[0].co < ex[1].co? 1:0;
        long long cost = 0;
        struct node l,r;
        l.num = '2', l.sum = 0;
        r.num = '2', r.sum = 0;
        for(int i=0;i<n;i++){
           if(s[i] == '?'){   
               int flag = 1, j;
               struct node x;
                x.num = '2', x.sum = 0;
              for(j=i+1;j<n;j++){
                if(s[j]=='?') {
                    j--;break;
                }
                if(s[j] != x.num){
                    if(!flag){
                        j--;break;
                    } 
                    else{
                        x.num = s[j]; x.sum=1; flag = 0;
                    }
                }
                else{
                    x.sum++;
                }
              }
              if(i==0){
                if(x.sum+1 < ex[mini].li && x.num == mini+'0'){
                    cost += ex[mini].co; x.sum++;
                }
                else if(x.num != mini + '0'){
                    cost += ex[mini].co;
                    if(x.num=='2') x.num = mini+'0',x.sum = 1;
                }
                else if(x.num == maxi + '0'){
                    cost += ex[maxi].co;x.sum++;
                } 
                else {
                    cost += ex[maxi].co;
                    if(x.num=='2') x.num = maxi+'0',x.sum =1;
                }
              }
              else if(i==n-1){
                if(l.num != mini+'0' || l.sum+1 < ex[mini].li){
                    cost += ex[mini].co;
                }  
                else{
                    cost += ex[maxi].co;
                }
             }  
             else{  
                int test = true;
                if(l.num == mini+'0' && l.sum +1 >= ex[mini].li || x.num == mini+'0' && x.sum+1 >= ex[mini].li){
                    test = false;
                }  
                if(l.num == x.num && l.num == mini + '0' && l.sum + x.sum + 1 >= ex[mini].li){
                    test = false;
                }
                //cout<<test<<endl;
                if(test){
                   if(x.num == '2') x.num =mini + '0' , x.sum = 0;
                   cost += ex[mini].co; 
                   if(x.num == mini + '0'){
                      x.sum+=1; 
                      if(l.num == mini + '0') x.sum+=l.sum;
                   }  
                }
                else{
                    if(x.num == '2') x.num=maxi+'0', x.sum = 0;
                    cost += ex[maxi].co;
                    if(x.num == '2') x.num = maxi+'0';
                    if(x.num == maxi +'0'){
                        x.sum+=1;
                        if(l.num == maxi+'0') x.sum+=l.sum;
                    } 
                }   
              }
              //cout<<l.num <<" "<<l.sum<<endl;
              //cout<<x.num<<" "<<x.sum<<endl<<endl;
              l = x;
              i = j;
            }
           else{
            if(s[i] != l.num) {
                l.num = s[i], l.sum = 1;
            }
            else  l.sum++;
          }
        }
       printf("%lld\n",cost);
   }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值