Mathison and pangrams: MATPAN

Mathison and pangrams Codechef solution in C, C++, Java, and Python

Public Submission link: Click here (anyone can submit here )

CU Submission link: Click here (need login by CU account)


Read problems statements in mandarin chineserussian and vietnamese as well.

Mathison recently inherited an ancient papyrus that contained some text. Unfortunately, the text was not a PANGRAM. Now, Mathison has a particular liking for holoalphabetic strings and the text bothers him. The good news is that Mathison can buy letters from the local store in order to turn his text into a pangram.

However, each letter has a price and Mathison is not very rich. Can you help Mathison find the cheapest way to obtain a pangram?

Input

The first line of the input file will contain one integer, T, representing the number of tests.

Each test will be formed from two lines. The first one contains 26 space-separated integers, representing the prices of all letters. The second will contain Mathison’s initial text (a string of N lowercase letters).

Output

The output file will contain T lines, one for each test. Each line will contain the answer for the corresponding test.

Constraints and notes

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 50,000
  • All prices are natural numbers between 1 and 1,000,000 (i.e. 106).
  • A pangram is a string that contains every letter of the Latin alphabet at least once.
  • All purchased letters are added to the end of the string.

Subtaks

Subtask #1 (30 points):

  • N = 1

Subtask #2 (70 points):

  • Original constraints

Sample Input 1 

2
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
abcdefghijklmopqrstuvwz
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
thequickbrownfoxjumpsoverthelazydog

Sample Output 1 

63
0

Explanation

First test
There are three letters missing from the original string: n (price 14), x (price 24), and y (price 25).
Therefore the answer is 14 + 24 + 25 = 63.

Second test
No letter is missing so there is no point in buying something. The answer is 0.

Mathison and pangrams Codechef solution in C

#include <stdio.h>
#include <string.h>

int main(void) {
	// your code goes here
	int t,n,cost[26];
	char str[50001];
	scanf("%d",&t);
	int arr[26];
	int sum;
	while(t--)
	{
	    sum = 0;
	    for(int i=0;i<26;i++)
	    scanf("%d",&cost[i]);
	    scanf("%s",str);
	    n = strlen(str);
	    for(int i=0;i<26;i++)
	    {
	        arr[i] = 0;
	    }
	    for(int i=0;i<n;i++)
	    {
	        arr[str[i] - 'a']++;
	    }
	    for(int i=0;i<26;i++)
	    {
	        if(arr[i] == 0)
	        sum += cost[i];
	    }
	    printf("%d\n",sum);
	}
	return 0;
}

Mathison and pangrams Codechef solutionin C++ 14

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
	    int arr[26];
	    int a=0,cost=0;
	    for(int i=0;i<26; i++){
	        cin>>arr[i];
	    }
	    string s;
	    cin>>s;
	    for(int i='a'; i<='z'; i++){
	        bool  b=true;
	        for(int j=0; j<s.size(); j++){
	            if(i==s[j]){
	                b=false;
	                break;
	            }
	        }
	        if(b) cost+=arr[a];
	        a++;
 	    }
	    cout<<cost;
	    cout<<endl;
	}
	return 0;
}

Mathison and pangrams Codechef solution in Python 3

t=int(input())
for i in range(t):
    cost=0
    menu=list(map(int,input().split()))
    word=input()

    for ch in "abcdefghijklmnopqrstuvwxyz":
        if ch not in word:
            cost=cost+menu[ord(ch)-ord('a')]
    print(cost)

Mathison and pangrams Codechef solution in 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
	{
	    Scanner scn = new Scanner(System.in);
		int t=scn.nextInt();
		int cost[]=new int[26];
		int h[]=new int[26];
		String s;
		while(t-->0) {
		    for(int i=0;i < 26;i++) {
		        cost[i]=scn.nextInt();
		        h[i]=0;
		    }
		    s=scn.next();
		    for(int i=0;i < s.length();i++)
		        h[s.charAt(i)-'a']++;
		    int ans=0;
		    for(int i=0;i < 26;i++) {
		        if(h[i]==0)
		        ans+=cost[i];
		    }
		    System.out.println(ans);
		}
	}
}

Mathison and pangrams Codechef solution


Mathison and pangrams Codechef solution
Mathison and pangrams Codechef solution
See also  Testing Robot : TSTROBOT