This weekend the qualification round of the Facebook Hacker Cup 2013 took place. Below you’ll find the first problem:
———-
When John was a little kid he didn’t have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could… he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.
Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.
The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn’t care about whether letters are uppercase or lowercase, so that doesn’t affect the beauty of a letter. (Uppercase ‘F’ is exactly as beautiful as lowercase ‘f’, for example.)
You’re a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?
Input
The input file consists of a single integer m followed by m lines.
Output
Your output should consist of, for each test case, a line containing the string “Case #x: y” where x is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum beauty for that test case.
Constraints
5 ≤ m ≤ 50
2 ≤ length of s ≤ 500
Sample Input
5
ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please 🙂
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves
Sample Output
Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646
———-
My Solution
The first problem is usually the easiest one, and this was the case here. All you had to do was to count how many times each letter appeared, and then assign 26 points to the most frequent one, 25 to the next most frequent one and so on.
#include <stdio.h>
int letters[26];
int main(){
int i,j,k;
scanf("%d",&j);
char c;
int total;
int max,position,beauty;
c = getchar();
for (i=0;i<j;i++){
beauty = 26;
total = 0;
for(k=0;k<26;k++)
letters[k] = 0;
while((c=getchar())!=10){
if(c>=65&&c<=90)
letters[c-65]++;
else if(c>=97&&c<=122)
letters[c-97]++;
}
while(1){
max = 0;
position = 0;
for(k=0;k<26;k++){
if(letters[k]>max){
max = letters[k];
position = k;
}
}
if(max>0){
letters[position] = 0;
total += max * beauty;
beauty--;
}
else{
break;
}
}
if(i>0)
printf("n");
printf("Case #%d: %d",i+1,total);
}
return 0;
}