Facebook Hacker Cup 2013 Solutions

Well, I was trying to participate in the facebook hacker cup. However, the deadlines have long since crossed. So I have listed my solutions to the problems to help out anyone who got stuck.

The questions are here : http://www.facebook.com/hackercup/problems.php?pid=403525256396727&round=185564241586420

//Solution to Problem 1 aka Beautiful Strings

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main()
{
int i,j,k,l,num,sum;
int all[26];
string line,dummy;
cin>>num;
std::getline(cin, dummy);

// A cin before a getline causes the first string to usually be empty.

//So added simple hack. GOOGLE Why!

for (i=0;i<26;i++) {
all[i]=0;
}
for (j=0; j < num; j++) {
line=””;
std::getline(cin, line);

// Getline is needed since strings may contain spaces

cout<<line<<endl;
for (i=0;i<26;i++)
all[i]=0;
sum=0;
for(k = 0; k < line.length(); k++)
{
l=line[k];
if((l>=65 && l<=90) || (l>=97 && l<=122))
{
if(l>=97)
l=l-97;
else
l=l-65;
all[l]++;
}
else
continue;
}
std::sort(all,all+26);
for(k=25;k>=0;k–) {
sum = sum + (k+1)*all[k];
}
cout << “Case #”<<j<<”: “<<sum << endl;
}
}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: