-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary_String_Cost.cpp
More file actions
56 lines (52 loc) · 1.41 KB
/
Copy pathBinary_String_Cost.cpp
File metadata and controls
56 lines (52 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#define ll long long
#define f(end) for(int i=0;i<end;i++)
#define e '\n'
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,n,x,y;
cin>>t;
while(t--)
{
cin>>n>>x>>y;
string s;
int zero=0,one=0;
cin>>s;
// One Approach
// sort(s.begin(),s.end());
// if(x<=y) xOp=true;
// else yOp=true;
// if(yOp){
// reverse(s.begin(),s.end());
// for(int i=0;i<n-1;i++){
// if(s[i]==1&&s[i+1]==0){
// cout<<y<<e;
// break;
// }
// }
// }
// else{
// for(int i=0;i<n-1;i++){
// if(s[i]==0&&s[i+1]==1){
// cout<<x<<e;
// break;
// }
// }
// }
// Second Approach
f(n){
if(s[i]=='0') zero++;
else one++;
}
if(one==n || zero==n) cout<<0<<e;
else cout<<min(x,y)<<e;
}
return 0;
}
// if chef can rearrange binary string then we can do then always sort or reverse sorted according to the min value of x & y
// ex s=100110 x=3 ,y=4
// we know that x is min so we sort it we get 000111 ,here the occurance of 01 is only 1 so we printed x
// similary , it happens with y so we print the min of x and y
// for 0 case of there is no combination of 0 and 1 then answer will be 0