Advertisement
Alisator

pal

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