Facebook Hacker Cup - Alphabet Soup

Problem statement:

Alfredo Spaghetti really likes soup, especially when it contains
alphabet pasta. Every day he constructs a sentence from letters,
places the letters into a bowl of broth and enjoys delicious alphabet
soup.

Today, after constructing the sentence, Alfredo remembered that the
Facebook Hacker Cup starts today! Thus, he decided to construct the
phrase "HACKERCUP". As he already added the letters to the broth, he
is stuck with the letters he originally selected. Help Alfredo
determine how many times he can place the word "HACKERCUP"
side-by-side using the letters in his soup.

Input
The first line of the input file contains a single integer T: the
number of test cases. T lines follow, each representing a single test
case with a sequence of upper-case letters and spaces: the original
sentence Alfredo constructed.

Output
Output T lines, one for each test case. For each case, output "Case
#t: n", where t is the test case number (starting from 1) and n is the
number of times the word "HACKERCUP" can be placed side-by-side using
the letters from the sentence.

Constraints
1 < T ≤ 20
Sentences contain only the upper-case letters A-Z and the space character
Each sentence contains at least one letter, and contains at most 1000
characters, including spaces
Example input
5
WELCOME TO FACEBOOK HACKERCUP
CUP WITH LABEL HACKERCUP BELONGS TO HACKER
QUICK CUTE BROWN FOX JUMPS OVER THE LAZY DOG
MOVE FAST BE BOLD
HACK THE HACKERCUP

Example output
Case #1: 1
Case #2: 2
Case #3: 1
Case #4: 0
Case #5: 1

Solution:

Very easy problem, just iterate through the sentence, and find least
occurrence in the alphabets contained in "HACKERCUP" (notice there are
2 Cs).

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Alphabet {
       public static void main(String[] args) throws FileNotFoundException {
               Scanner sc = new Scanner (new File(args[0]));
               int t = sc.nextInt();
               sc.nextLine();
               for (int i=1; i<= t; i++) {
                       String s = sc.nextLine();
                       char[] ch = s.toCharArray();
                       int[] al = new int[7];
                       int c = 0;
                       for (int i1=0; i1<ch.length; i1++) {
                               switch (ch[i1]) {
                               case 'H': al[0]++;
                               break;
                               case 'A': al[1]++;
                               break;
                               case 'C': c++;
                               break;
                               case 'K': al[2]++;
                               break;
                               case 'E': al[3]++;
                               break;
                               case 'R': al[4]++;
                               break;
                               case 'U': al[5]++;
                               break;
                               case 'P': al[6]++;
                               break;
                               default: break;
                               }
                       }
                       int min = c/2;
                       for (int j=0; j<al.length; j++) {
                               if (al[j]<min)
                                       min = al[j];
                       }
                       System.out.println("Case #"+i+": "+min);
               }
       }

}

 

Bo Tian

Bo Tian


Archive

2012 (3)
2011 (46)
2010 (62)
Posterous theme by Cory Watilo