Sunday, September 05, 2004
SRM 209 DIV 2 - 500 pts
I dint saved the solution for 250 point. But 500 point was also good. Heres the problem statement. I could not complete 900 point problem.
The Olympic Games in Athens end tomorrow. Given the results of the olympic disciplines, generate and return the medal table. The results of the disciplines are given as a String[] results, where each element is in the format "GGG SSS BBB". GGG, SSS and BBB are the 3-letter country codes (three capital letters from 'A' to 'Z') of the countries winning the gold, silver and bronze medal, respectively. The medal table is a String[] with an element for each country appearing in results. Each element has to be in the format "CCO G S B" (quotes for clarity), where G, S and B are the number of gold, silver and bronze medals won by country CCO, e.g. "AUT 1 4 1". The numbers should not have any extra leading zeros. Sort the elements by the number of gold medals won in decreasing order. If several countries are tied, sort the tied countries by the number of silver medals won in decreasing order. If some countries are still tied, sort the tied countries by the number of bronze medals won in decreasing order. If a tie still remains, sort the tied countries by their 3-letter code in ascending alphabetical order.Definition
Heres my solution
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;
class Country
{
String name;
int gold;
int silver;
int bronze;
Country()
{
name = "";
gold = silver = bronze = 0;
}
public String toString()
{
return (name + " " + gold + " " + silver + " " + bronze);
}
public int compareTo( Country other )
{
if( gold > other.gold )
{
return 1; //this come first
}
else if( gold < other.gold )
{
return -1;
}
if( silver > other.silver)
{
return 1; //this come first
}
else if( silver < other.silver )
{
return -1;
}
if( bronze > other.bronze )
{
return 1; //this come first
}
else if( bronze < other.bronze )
{
return -1;
}
return -1*name.compareTo(other.name);
}
}
public class MedalTable
{
public String[] generate(String[] results)
{
Hashtable cnames = new Hashtable();
for(int i = 0; i < results.length; i++)
{
StringTokenizer st = new StringTokenizer( results[i], " " );
while( st.hasMoreTokens() )
{
cnames.put( st.nextToken(), "" );
}
}
Country cns[] = new Country[ cnames.size() ];
Enumeration en = cnames.keys();
int i = 0;
for(; en.hasMoreElements(); )
{
cns[i] = new Country();
cns[i].name = "" + en.nextElement();
System.out.println(cns[i].name);
i++;
}
for(int k = 0; k < results.length; k++)
{
StringTokenizer st = new StringTokenizer( results[k], " " );
while( st.hasMoreTokens() )
{
String coun = "" + st.nextElement();
for( int j = 0; j < cns.length; j++ )//for gold
{
if( cns[j].name.equals(coun) )
{
cns[j].gold++;
}
}
coun = "" + st.nextElement();
for( int j = 0; j < cns.length; j++ )//for silver
{
if( cns[j].name.equals(coun) )
{
cns[j].silver++;
}
}
coun = "" + st.nextElement();
for( int j = 0; j < cns.length; j++ )//for bronze
{
if( cns[j].name.equals(coun) )
{
cns[j].bronze++;
}
}
}
}
sortCountry( cns );
String out[] = new String[ cns.length ];
for( int k = 0; k < out.length;k++ )
{
out[k ] = cns[k].toString();
}
return out;
}
private void sortCountry( Country cns[] )
{
for( int i = 0; i < cns.length; i++ )
{
for( int j = i + 1; j < cns.length; j++ )
{
if( cns[i].compareTo( cns[j] ) < 0 )
{
Country temp = new Country();
temp = cns[i];
cns[i] = cns[j];
cns[j] = temp;
}
}
}
}
}

