链接 :
思路 :
双指针
对于每一段夹在1之间的0,求其长度,如果len*b<=a,那么可以用地雷先铺满这一段路;
代码 :
// https://codeforces.com/problemset/problem/1443/B
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;
inline void solve(){
int a , b ; cin >> a >> b ;
string s ; cin >> s ;
int n = s.size() ;
s = ' ' + s ;
LL ans = 0 ;
int i = 1 ;
while(i<=n && s[i] == '0') i++ ;
int j = n ;
while(j>=1&&s[j]=='0') j-- ;
if(j<i){
cout << 0 << endl ;
return ;
}
for(;i<=j;i++){
if(s[i]=='1') {
while(i<=j && s[i]=='1') i++ ;
i--;
ans += a ;
}else{
int r = i ;
while(r<=j&&s[r]=='0') r++ ;
int len = r - i ;
if(len * b <= a){
ans += len*b-a;
}
i = r - 1 ;
}
}
cout << ans << endl ;
}
signed main(){
IOS
int _ = 1;
cin >> _;
while(_ --) solve();
return 0;
}