Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pal;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class Main {
- static public int countBelts; // belts are labeled 0:countBelts-1
- static public int countSegment; //labeled by chars a-z, Segment in 1 disk
- static public int countDisk; // count disks in one belt
- static public String[] belts;
- static public String[] beltsGroups;
- static public ArrayList<String> beltsUnique;
- static public ArrayList<String> disks;
- static public int countGroups;
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String[] in = br.readLine().split(" ");
- countBelts = Integer.parseInt(in[0]);
- countSegment = Integer.parseInt(in[1]);
- countDisk = Integer.parseInt(in[2]);
- belts = new String[countBelts];
- beltsUnique = new ArrayList<>();
- disks = new ArrayList<>();
- beltsGroups = new String[countBelts];
- countGroups = -1;
- String belt, group;
- for (int i = 0; i < countBelts; i++) {
- in = br.readLine().split(" ");
- belt = createBeltofDiskIndex(in);
- belts[i] = belt;
- }
- for (int i = 0; i < countBelts; i++) {
- belt = createBeltGroupsofBeltIndex(belts[i]);
- if(beltsGroups[countGroups] == null) {beltsGroups[countGroups] = "";}
- beltsGroups[countGroups] += belt;
- // System.out.print("belt " + belt + " " + beltsGroups[countGroups] + " ");
- }
- for (int i = 0; i < countGroups+1; i++) {
- System.out.print(beltsGroups[i] + " ");
- }
- }
- public static String createBeltofDiskIndex(String[] strArr) {
- Integer indexOfSegment;
- String disk;
- String belt = "";
- for (int j = 1; j <= countDisk; j++) {
- disk = strArr[j];
- // System.out.println("disk " +disk);
- indexOfSegment = indexOfexistingDisk(disk);
- if (indexOfSegment >= 0) { //vzor jiz existuje
- belt += indexOfSegment.toString() + " ";
- } else //vzor neexistuje, pridame ho do listu a vratime si jeho index
- {
- disks.add(disk + disk);
- indexOfSegment = disks.indexOf(disk + disk);
- belt += indexOfSegment.toString() + " ";
- }
- }
- return belt;
- }
- public static String createBeltGroupsofBeltIndex(String belt) {
- Integer indexOfSegment;
- String beltUniq = "";
- indexOfSegment = indexOfexistingBelt(belt);
- if (indexOfSegment >= 0) { //pasek jiz existuje
- beltUniq = indexOfSegment.toString() + " ";
- } else //vzor pasku neexistuje, pridame ho do listu a vratime si jeho index
- {
- beltsUnique.add(belt + belt);
- indexOfSegment = beltsUnique.indexOf(belt + belt);
- beltUniq = indexOfSegment.toString() + " ";
- countGroups++;
- }
- return beltUniq;
- }
- public static int indexOfexistingDisk(String disk) {
- if (disks.isEmpty()) {
- return -1;
- } else {
- for (String s : disks) {
- if (checkRotation(s, disk) || checkRotation(s, flip(disk))) {
- return disks.indexOf(s);
- }
- }
- }
- return -1;
- }
- public static int indexOfexistingBelt(String belt) {
- if (beltsUnique.isEmpty()) {
- return -1;
- } else {
- for (String s : beltsUnique) {
- if (checkRotation(s, belt) || checkRotation(s, flip(belt))) {
- return beltsUnique.indexOf(s);
- }
- }
- }
- return -1;
- }
- public static boolean checkRotation(String s1, String s2) {
- //vrati true, kdyz string1 obsahuje string 2
- if ((s1.length() == 2 * s2.length()) && (s1.contains(s2))) {
- return true;
- } else {
- return false;
- }
- }
- //zrcadleni
- public static String flip(String s1) {
- StringBuilder str = new StringBuilder(s1);
- return str.reverse().toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement