Facebook hacker cup 2013 (Beautiful Strings Problem)

By | January 28, 2013

I tried to make it in c++, but faced some difficulties during the input interpretation of file…..since didn’t understood that how they are separating lines…

 

// Code is..

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

int beauty(char *a)
{
char x[180][2];
cout<<a;
int i=0, j=0;
int l=strlen(a);
int k=0;
for(i=0; i<l; i++)
{
k=0;
if((a[i]>=65 && a[i]<=90 ) || (a[i]>=97 && a[i]<=122 ) )
{
for(j=0;j<l;j++)
{
if( a[i]==a[j] || a[i]==a[j]+32 || a[i]==a[j]-32 )
k++;
}
x[i][0]=a[i];
x[i][1]=k+48;
}
else
{
x[i][0]=’ ‘;
x[i][1]=’ ‘;
}
}

/*

testing inputs
cout<<endl;
for(i=0;i<l;i++)
{
for(j=0;j<=1;j++)
{
cout<<x[i][j]<<” “;
}
cout<<endl;
}
*/

// operations

int max=0;
char tmp;
char tmp2;
int kk=26;
int sumf=0;
for(int z=0;z<l;z++)
{
int max=0;
int sum=0;
// get max and char
for(int t=0;t<l;t++)
{
if(atoi(&x[t][1])>max)
{
max=atoi(&x[t][1]);
tmp=x[t][0];
}
}
//sum of max occurences
// cout<<endl<<“Phase “<<kk<<“max “<<max;
for(int t=0;t<l;t++)
{
if ( ( atoi(&x[t][1])==max) && ((x[t][0]==tmp) || ((x[t][0])+32==tmp) || ((x[t][0])-32==tmp) ) )
{
sum=sum + kk;
x[t][0]=’?’;
// cout<<endl<<“x[t][0]=’ ‘;=”<<x[t][0];
x[t][1]=48;
// cout<<endl<<“x[t][1]=’ ‘;=”<<x[t][1];
}
}
kk–;
sumf=sumf+sum;
}
/*
cout<<endl;
for(i=0;i<l;i++)
{
for(j=0;j<=1;j++)
{
cout<<x[i][j]<<” “;
}
cout<<endl;
}
*/
return(sumf);
}
//end beauty

int main()
{

char q[280];
char fp;
char tt[180];
int yy;

ifstream fin;
ofstream fout;
int tyu;
fin.open(“beautiful_stringstxt.txt”);
fout.open(“output.txt”);
while (!fin.eof())
{
static int i=0;
fin.getline(tt, sizeof(tt));
yy=beauty(tt);
cout<<endl;
fout<<“Case #”<<i<<“: “<<yy<<‘\n’;
i++;
}
fin.close();
fout.close();

return 0;
}