Linux Tips and Tricks

Linux Tips and Tricks

Facebook Hacker Cup 2012 – Alphabet Soup solution

January26

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

1
2
3
4
5
6
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

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

Original Input File:

Download the original input file from here

Output for Original File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Case #1: 82
Case #2: 1
Case #3: 15
Case #4: 15
Case #5: 0
Case #6: 2
Case #7: 58
Case #8: 3
Case #9: 13
Case #10: 11
Case #11: 17
Case #12: 10
Case #13: 24
Case #14: 76
Case #15: 48
Case #16: 2
Case #17: 60
Case #18: 1
Case #19: 49
Case #20: 3

Shell Script
If you are not getting the desired output then use nawk or gawk

1
2
3
4
5
6
#!/usr/bin/ksh
fileName="$1"
awk 'NR>1' $1 | while read line
do
	echo $line | nawk '/H/&&/A/&&/C/&&/K/&&/E/&&/R/&&/C/&&/U/&&/P/' | fold -w1 | sort | uniq -c | nawk '{if($0~/C$/){a=$1/2}else{a=$1}printf("%i %s\n",a,$2)}'| sort -n | egrep "H|A|C|K|E|R|C|U|P" > out.txt && nawk  '{if(NR==1){print $1}}' out.txt  || echo "0"
done | awk '{printf("Case #%s: %s\n",NR,$1)}'
posted under Uncategorized | No Comments »

Recent Comments

    Categories