1107 - Niro loves Chinese

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ACM__dongsheng/article/details/64906590

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;
}

猜你喜欢

转载自blog.csdn.net/ACM__dongsheng/article/details/64906590