Travel Pass | CodeChef Solution

Hello coders, today we are going to solve Travel Pass CodeChef Solution whose Problem Code is TRAVELPS.

Task

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

His journey is given to you as a binary string S of length N where 0 denotes crossing from one district to another district (which needs an inter-district e-pass), and a 1 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 T denoting the number of test cases. The description of T test cases follows.
  • Each test case contains two lines of input.
  • First line contains three space separated integers N,A and B.
  • Second line contains the string S.

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

  • 1 ≤ T ≤ 102
  • 1 ≤ N, A, B ≤ 102
  • 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 1

 2
 2
 5 

Explanation

Test case 1: Chef needs total 2 inter-district e-passes, and he will be filling them in total 1 ⋅ 2 = 2 minutes.

Test case 3: Chef needs total 1 inter-district e-pass and 3 inter-state e-passes, and he will be filling them in total 2 ⋅ 1 + 1 ⋅ 3 = 5 minutes.

Solution – Travel Pass

C++

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;cin>>t;
	while(t--){
	    int n,a,b; cin>>n>>a>>b;
	    string s; cin>>s;
	    int count0=0,count1=0;
	    for(int i=0;i<n;i++){
	        if(s[i]=='0')count0++;
	        else count1++;
	    }
	    cout << (a*count0 + b*count1) <<"\n";
	}
	return 0;
}

Python

# cook your dish here
T = int(input())
for i in range(T):
    n, a, b = map(int, input().split())
    
    string = input()
    
    count_1 = string.count('1')
    count_0 = string.count('0')
    
    total_cost = count_1 * b + count_0 * a
    print(total_cost)
    

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- > 0){
		    int n,a,b;
		    n = sc.nextInt();
		    a = sc.nextInt();
		    b = sc.nextInt();
		    sc.nextLine();
		    String s = sc.nextLine();
		    char[] charArr = s.toCharArray();
		    int countZero=0,countOne=0;
		    for(int i=0;i<n;i++){
		        if(charArr[i]=='0')countZero++;
		        else countOne++;
		    }
		    System.out.println(a*countZero + b*countOne);
		}
	}
}

Disclaimer: The above Problem (Travel Pass) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *