Advertisement
Alisator

PAL4_23-11_newVersion

Nov 23rd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 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.  
  19.     public static void main(String[] args) throws IOException {
  20.  
  21.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22.         String[] in = br.readLine().split(" ");
  23.         countBelts = Integer.parseInt(in[0]);
  24.         countSegment = Integer.parseInt(in[1]);
  25.         countDisk = Integer.parseInt(in[2]);
  26.         belts = new String[countBelts];
  27.         beltsUnique = new ArrayList<>();
  28.         disks = new ArrayList<>();
  29.         beltsGroups = new String[countBelts];
  30.         countGroups = -1;
  31.         String belt, group;
  32.         for (int i = 0; i < countBelts; i++) {
  33.             in = br.readLine().split(" ");
  34.             belt = createBeltofDiskIndex(in);
  35.             belts[i] = belt;
  36.         }
  37.  
  38.         for (int i = 0; i < countBelts; i++) {
  39.             belt = createBeltGroupsofBeltIndex(belts[i]);
  40.             if(beltsGroups[countGroups] == null) {beltsGroups[countGroups] = "";}
  41.              beltsGroups[countGroups] += belt;
  42.            //   System.out.print("belt " + belt + " " + beltsGroups[countGroups] + " ");
  43.         }
  44.        
  45.        
  46.           for (int i = 0; i < countGroups+1; i++) {
  47.                System.out.print(beltsGroups[i] + " ");
  48.           }
  49.        
  50.        
  51.  
  52.     }
  53.  
  54.     public static String createBeltofDiskIndex(String[] strArr) {
  55.         Integer indexOfSegment;
  56.         String disk;
  57.         String belt = "";
  58.         for (int j = 1; j <= countDisk; j++) {
  59.             disk = strArr[j];
  60.             //   System.out.println("disk " +disk);
  61.             indexOfSegment = indexOfexistingDisk(disk);
  62.             if (indexOfSegment >= 0) { //vzor jiz existuje
  63.                 belt += indexOfSegment.toString() + " ";
  64.             } else //vzor neexistuje, pridame ho do listu a vratime si jeho index
  65.             {
  66.                 disks.add(disk + disk);
  67.                 indexOfSegment = disks.indexOf(disk + disk);
  68.                 belt += indexOfSegment.toString() + " ";
  69.             }
  70.         }
  71.         return belt;
  72.     }
  73.  
  74.     public static String createBeltGroupsofBeltIndex(String belt) {
  75.         Integer indexOfSegment;
  76.         String beltUniq = "";
  77.         indexOfSegment = indexOfexistingBelt(belt);
  78.         if (indexOfSegment >= 0) { //pasek jiz existuje
  79.             beltUniq = indexOfSegment.toString() + " ";
  80.         } else //vzor pasku neexistuje, pridame ho do listu a vratime si jeho index
  81.         {
  82.             beltsUnique.add(belt + belt);
  83.             indexOfSegment = beltsUnique.indexOf(belt + belt);
  84.             beltUniq = indexOfSegment.toString() + " ";
  85.             countGroups++;
  86.         }
  87.         return beltUniq;
  88.     }
  89.  
  90.     public static int indexOfexistingDisk(String disk) {
  91.         if (disks.isEmpty()) {
  92.             return -1;
  93.         } else {
  94.             for (String s : disks) {
  95.                 if (checkRotation(s, disk) || checkRotation(s, flip(disk))) {
  96.                     return disks.indexOf(s);
  97.                 }
  98.             }
  99.         }
  100.         return -1;
  101.     }
  102.  
  103.     public static int indexOfexistingBelt(String belt) {
  104.         if (beltsUnique.isEmpty()) {
  105.             return -1;
  106.         } else {
  107.             for (String s : beltsUnique) {
  108.                 if (checkRotation(s, belt) || checkRotation(s, flip(belt))) {
  109.                     return beltsUnique.indexOf(s);
  110.                 }
  111.             }
  112.         }
  113.         return -1;
  114.     }
  115.  
  116.     public static boolean checkRotation(String s1, String s2) {
  117.         //vrati true, kdyz string1 obsahuje string 2
  118.         if ((s1.length() == 2 * s2.length()) && (s1.contains(s2))) {
  119.             return true;
  120.         } else {
  121.             return false;
  122.         }
  123.     }
  124.  
  125.     //zrcadleni
  126.     public static String flip(String s1) {
  127.         StringBuilder str = new StringBuilder(s1);
  128.         return str.reverse().toString();
  129.     }
  130.  
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement