Computer Programming Contest Preparation

ToolBox - Source for: 112/11286/Conformity.java



/home/toolbox/public_html/solutions/112/11286/Conformity.java
    1 /* Problem 11286 - Conformity
    2  * Stolen with permission by: Jason Kincl
    3  * With optimization from Chris Miceli :)
    4  * March 3, 2009
    5  */
    6 
    7 import java.util.Scanner;
    8 import java.util.Arrays;
    9 import java.util.HashMap;
   10 
   11 public class Conformity
   12 {
   13 
   14     public static void main(String[] args)
   15     {
   16         Scanner in = new Scanner(System.in);
   17         HashMap<Long, Integer> map = new HashMap<Long, Integer>();
   18 
   19         int n = in.nextInt(); // read in number of frosh
   20 
   21         // read in the input
   22         while (n != 0)
   23             {
   24                 int max = 1;
   25                 for (int i = 0; i < n; i++)
   26                     {
   27                         int[] line = new int[5];
   28                         line[0] = in.nextInt();
   29                         line[1] = in.nextInt();
   30                         line[2] = in.nextInt();
   31                         line[3] = in.nextInt();
   32                         line[4] = in.nextInt();
   33                         Arrays.sort(line);
   34 
   35                         // concatenate line elements into one line for easy comparison
   36                         long total = 0;
   37                         for (int y = 0; y < 5; y++)
   38                             {
   39                                 total = total*1000 + line[y];
   40                             }
   41                         // check to see if line is in map already
   42                         // if it is, increment its value by 1
   43                         // else add new line
   44                         if (map.containsKey(total))
   45                             {
   46                                 int value = map.get(total) + 1; // CACHE the value, necessary for speed up
   47                                 map.put(total, value);
   48                                 if (max < value)
   49                                     max = value;
   50                             }
   51                         else
   52                             map.put(total, 1);
   53                     }
   54                 //System.out.println(map.toString());
   55                 //System.out.printf("max: %d%n",max);
   56 
   57                 // find if there are other keys with same max
   58                 int answer = 0;
   59                 for (int value : map.values())
   60                     {
   61                         if (max == value)
   62                             answer += max;
   63                     }
   64                 System.out.println(answer);
   65 
   66                 n = in.nextInt(); // read in number of frosh
   67                 map.clear();
   68             }
   69     }
   70 }
   71