Codechef September long challenge 2021 | Travel Pass Solution

 

Codechef September long challenge 2021 | Travel Pass Solution

Chef is going on a road trip and needs to apply for inter-district and inter-state travel e-passes. It takes AA minutes to fill each inter-district e-pass application and BB minutes for each inter-state e-pass application.

His journey is given to you as a binary string SS of length NN where 00 denotes crossing from one district to another district (which needs an inter-district e-pass), and a 11 denotes crossing from one state to another (which needs an inter-state e-pass).

Find the total time Chef has to spend on filling the various forms.

Input Format

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • Each test case contains two lines of input.
  • First line contains three space separated integers N,AN,A and BB.
  • Second line contains the string SS.

Output Format

For each testcase, output in a single line the total time Chef has to spend on filling the various forms for his journey.

Constraints

  • 1T1021≤T≤102
  • 1N,A,B1021≤N,A,B≤102
  • Si{0,1}Si∈{′0′,′1′}

Subtasks

Subtask #1 (100 points): original constraints

Sample Input 1 

3
2 1 2
00
2 1 1
01
4 2 1
1101

Sample Output 

2
2
5

C++ Solution

#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, a, b,ca=0,cb=0;
cin >> n >> a >> b;
string s;
cin>>s;
for (int i = 0; i < n; i++)
{
if(s[i]=='0')
ca++;
else
cb++;
}
int ans=0;
ans=((ca*a)+(cb*b));
cout<<ans<<endl;
}
return 0;
}


Python Solution

for _ in range(int(input())):
N, A, B = map(int, input().split())
S = [int(i) for i in str(input())]
count1 = 0
count2 = 0
for i in S:
if i==0:
count1 = count1 + 1
else:
count2 = count2 + 1
print(count1*A + count2*B)

 

Java Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
String s = sc.next();
int totalA = 0, totalB = 0;
for(int i = 0;i<n;i++)
{
if(s.charAt(i)=='0')
totalA+=a;
else
totalB+=b;
}
System.out.println(totalA+totalB);
}
}
}

 

 

Post a Comment

Previous Post Next Post