Advertisement
Alisator

PAL4_25-11

Nov 25th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7.  
  8. public class Main {
  9.  
  10.     static public int countBelts; // belts are labeled 0:countBelts-1
  11.     static public int countSegment; //labeled by chars a-z, Segment in 1 disk
  12.     static public int countDisk; // count disks in one belt
  13.     static public String[] belts;
  14.     static public String[] beltsGroups;
  15.     static public ArrayList<String> beltsUnique;
  16.     static public ArrayList<String> disks;
  17.     static public int countGroups;
  18.     static public int[] numOutput;
  19.     static public String[] strOutput;
  20.     public static void main(String[] args) throws IOException {
  21.  
  22.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  23.         String[] in = br.readLine().split(" ");
  24.         countBelts = Integer.parseInt(in[0]);
  25.         countSegment = Integer.parseInt(in[1]);
  26.         countDisk = Integer.parseInt(in[2]);
  27.         belts = new String[countBelts];
  28.         beltsUnique = new ArrayList<>();
  29.         disks = new ArrayList<>();
  30.         beltsGroups = new String[countBelts];
  31.         numOutput = new int[countBelts];
  32.         countGroups = -1;
  33.         String belt, group;
  34.  
  35.         for (int i = 0; i < countBelts; i++) {
  36.             in = br.readLine().split(" ");
  37.             belt = createBeltofDiskIndex(in);
  38.             belts[i] = belt;
  39.             belt = createBeltGroupsOfBeltIndex(belts[i], i);
  40.         }
  41. /*
  42.         for (int i = 0; i < countBelts; i++) {
  43.             belt = createBeltGroupsOfBeltIndex(belts[i], i);
  44.         }*/
  45.  
  46.        strOutput = new String[countGroups+1];
  47.  
  48.         countingSort();
  49.         System.out.println(countGroups+1);
  50.          for (int i = countGroups; i >= 0; i--) {
  51.              System.out.println(strOutput[i]);
  52.         }
  53.          
  54.  
  55.     }
  56.  
  57.     public static String createBeltofDiskIndex(String[] strArr) {
  58.         Integer indexOfSegment;
  59.         String disk;
  60.         String belt = "";
  61.         for (int j = 1; j <= countDisk; j++) {
  62.             disk = strArr[j];
  63.             //   System.out.println("disk " +disk);
  64.             indexOfSegment = indexOfexistingDisk(disk);
  65.              if(indexOfSegment == -1)  //vzor neexistuje, pridame ho do listu a vratime si jeho index
  66.             {
  67.                 disks.add(disk + disk);
  68.                 indexOfSegment = disks.indexOf(disk + disk);
  69.                 belt += indexOfSegment.toString() + " ";
  70.             }
  71.             else { //vzor jiz existuje
  72.                 belt += indexOfSegment.toString() + " ";
  73.             }
  74.         }
  75.         return belt;
  76.     }
  77.  
  78.     public static String createBeltGroupsOfBeltIndex(String belt, int numberOfBelt) {
  79.         Integer indexOfSegment;
  80.         String beltUniq = "";
  81.         indexOfSegment = indexOfexistingBelt(belt);
  82.         if (indexOfSegment >= 0) { //pasek jiz existuje
  83.             beltsGroups[indexOfSegment] = beltsGroups[indexOfSegment] + " " + numberOfBelt;
  84.             numOutput[indexOfSegment]++;
  85.  //           System.out.println(numOutput[countGroups] + " - " + beltsGroups[indexOfSegment][0]);
  86.         } else //vzor pasku neexistuje, pridame ho do listu a vratime si jeho index
  87.         {
  88.             beltsUnique.add(belt + belt);
  89.             countGroups++;
  90.             beltsGroups[countGroups] = Integer.toString(numberOfBelt);
  91.             numOutput[countGroups]++;
  92. //            System.out.println(numOutput[countGroups] + " - " + beltsGroups[countGroups][0]);
  93.            
  94.         }
  95.         return beltUniq;
  96.     }
  97.  
  98.     public static int indexOfexistingDisk(String disk) {
  99.         if (disks.isEmpty()) {
  100.             return -1;
  101.         } else {
  102.             for (String s : disks) {
  103.                 if (checkRotation(s, disk) || checkRotation(s, flip(disk))) {
  104.                     return disks.indexOf(s);
  105.                 }
  106.             }
  107.         }
  108.         return -1;
  109.     }
  110.  
  111.     public static int indexOfexistingBelt(String belt) {
  112.         if (beltsUnique.isEmpty()) {
  113.             return -1;
  114.         } else {
  115.             for (String s : beltsUnique) {
  116.                 if (checkRotation(s, belt) || checkRotation(s, flip(belt))) {
  117.                     return beltsUnique.indexOf(s);
  118.                 }
  119.             }
  120.         }
  121.         return -1;
  122.     }
  123.  
  124.     public static boolean checkRotation(String s1, String s2) {
  125.         //vrati true, kdyz string1 obsahuje string 2
  126.         if ((s1.length() == 2 * s2.length()) && (s1.contains(s2))) {
  127.             return true;
  128.         } else {
  129.             return false;
  130.         }
  131.     }
  132.  
  133.     //zrcadleni
  134.     public static String flip(String s1) {
  135.         StringBuilder str = new StringBuilder(s1);
  136.         return str.reverse().toString();
  137.     }
  138.  
  139.     public static void countingSort() {
  140.         int[] sortedArray = new int[countGroups+1];
  141.         int min = numOutput[0];
  142.         int max = numOutput[0];
  143.         //find min and max values for histogram from current column
  144.         for (int i = 1; i < countGroups+1; i++) {
  145.            
  146.  
  147.             if ( numOutput[i] < min) {
  148.                 min =  numOutput[i];
  149.             } else if ( numOutput[i] > max) {
  150.                 max =  numOutput[i];
  151.             }
  152.         }
  153.         //histogram length from min to max
  154.         int[] hist = new int[max - min + 1];
  155.         for (int i = 0; i < countGroups+1; i++) {
  156.             hist[numOutput[i] - min]++;
  157.         }
  158.         //pole vyskytu
  159.         hist[0]--;
  160.         for (int i = 1; i < hist.length; i++) {
  161.             hist[i] = hist[i] + hist[i - 1];
  162.         }
  163.         //min to max
  164.         for (int i = 0 ; i <= countGroups; i++) {
  165.             int histIndex =  numOutput[i] - min;
  166.             int index = hist[histIndex];
  167.             sortedArray[index] =  numOutput[i];
  168.             strOutput[index] = beltsGroups[i];
  169.             hist[histIndex]--;
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement