Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.90 KB | None | 0 0
  1. public class Matrix {
  2.     private int[][] matrix;
  3.     private ArrayList<String> list;
  4.     private String[] charac;
  5.     private int[][] elements;
  6.     private Map<Integer, ArrayList<Integer>> groupsMap = new HashMap<>();
  7.     private Map<Integer, String> groupToString = new HashMap<>();
  8.  
  9.  
  10.     public void setList(ArrayList<String> list) {
  11.         this.list = list;
  12.     }
  13.  
  14.  
  15.     public ArrayList<String> getList() {
  16.         return list;
  17.     }
  18.  
  19.     private String getCharacString() {
  20.         String summ = "";
  21.         for (String s : list) {
  22.             summ += s + " ";
  23.         }
  24.         return summ.trim();
  25.  
  26.     }
  27.  
  28.     public String getCharacterString() { // proverka
  29.         String[] split = getCharacString().split(" ");
  30.         String s = split[0];
  31.         for (int i = 1; i < split.length; i++) {
  32.             if (!s.contains(split[i])) s += " " + split[i];
  33.         }
  34.         return s;
  35.     }
  36.  
  37.     public void fillCharacElements() { // 1 : 0
  38.         int stringLength = getCharacterString().split(" ").length;
  39.         String[] split = getCharacterString().split(" ");
  40.         elements = new int[list.size()][stringLength];
  41.         for (int i = 0; i < list.size(); i++) {
  42.             for (int j = 0; j < stringLength; j++) {
  43.                 if (list.get(i).contains(split[j])) {
  44.                     elements[i][j] = 1;
  45.                 } else elements[i][j] = 0;
  46.             }
  47.         }
  48.  
  49.         System.out.println(getCharacterString());
  50.         for (int i = 0; i < list.size(); i++) {
  51.             for (int j = 0; j < stringLength; j++) {
  52.                 System.out.print(elements[i][j]);
  53.             }
  54.             System.out.println();
  55.         }
  56.     }
  57.  
  58.     public String elementsCastToString() {
  59.         String s = "";
  60.         for (int i = 0; i < elements.length; i++) {
  61.             for (int j = 0; j < elements[i].length; j++) {
  62.                 if (j % 3 == 0) s += elements[i][j] + "   ";
  63.                 else s += elements[i][j] + "    ";
  64.             }
  65.             s += "\n";
  66.         }
  67.         return s.trim();
  68.     }
  69.  
  70.     public String getMatrix() {
  71.         String s = "";
  72.         matrix = new int[list.size()][list.size()];
  73.         int summ = 0;
  74.         for (int i = 0; i < elements.length - 1; i++) {
  75.             int k = 1;
  76.             if (k == elements.length) break;
  77.             while (true) {
  78.                 for (int j = 0; j < elements[i].length; j++) {
  79.                     if (elements[i][j] == elements[i + k][j]) summ++;
  80.                 }
  81.                 matrix[k + i][i] = summ;
  82.                 matrix[i][k + i] = summ;
  83.                 summ = 0;
  84.                 k++;
  85.                 if (i + k == elements.length) break;
  86.             }
  87.  
  88.         }
  89.         for (int i = 0; i < matrix.length; i++) {
  90.             matrix[i][i] = -1;
  91.         }
  92.         for (int i = 0; i < matrix.length; i++) {
  93.             for (int j = 0; j < matrix[i].length; j++) {
  94.                 System.out.print(matrix[i][j] + " ");
  95.             }
  96.             System.out.println();
  97.         }
  98.  
  99.         for (int i = 0; i < matrix.length; i++) {
  100.             for (int j = 0; j < matrix[i].length; j++) {
  101.                 if (j % 3 == 0) s += matrix[i][j] + "   ";
  102.                 else s += matrix[i][j] + "    ";
  103.             }
  104.             s += "\n";
  105.         }
  106.         return s.trim();
  107.     }
  108.  
  109.     public String getGroup() {
  110.         Set<Integer> emptyRowsList = new LinkedHashSet<>();
  111.         ArrayList<Integer> freeRowsList = new ArrayList<>();
  112.         int numberGroup = 0;
  113.         int rowsCount = 0;
  114.         int max = matrix[0][0];
  115.         String result = "";
  116.  
  117.         for (int i = 0; i < matrix.length; i++) {
  118.             for (int j = 0; j < matrix[i].length; j++) {
  119.                 if (matrix[i][j] > max) max = matrix[i][j];
  120.             }
  121.         }
  122.  
  123.         for (int i = 0; i < matrix.length; i++) {
  124.             freeRowsList.add(i);
  125.         }
  126.  
  127.         do {
  128.             ArrayList<Integer> groupsNumber = new ArrayList<>();
  129.             Set<Integer> groupsNumbersSet = new LinkedHashSet<>();
  130.             outer:
  131.             for (int i = 0; i < matrix.length; i++) {
  132.                 for (int j = 0; j < matrix[i].length; j++) {
  133.                     if (matrix[i][j] == max && (freeRowsList.contains(i) || freeRowsList.contains(j))) {
  134.                         boolean isDuplicate = false;
  135.                         int duplicateInt = 0;
  136.                         groupsNumbersSet.add(i);
  137.                         groupsNumbersSet.add(j);
  138.                         int count;
  139.                         do {
  140.                             count = 0;
  141.                             groupsNumber = new ArrayList<>(groupsNumbersSet);
  142.                             for (int k : groupsNumber) {
  143.                                 for (int l = 0; l < matrix[k].length; l++) {
  144.                                     if (emptyRowsList.contains(l)) {
  145.                                         isDuplicate = true;
  146.                                         duplicateInt = l;
  147.                                     }
  148.                                     if (max == matrix[k][l] ) {
  149.                                         int size = groupsNumbersSet.size();
  150.                                         groupsNumbersSet.add(l);
  151.                                         int currentSize = groupsNumbersSet.size();
  152.                                         if (currentSize>size) count++;
  153.                                     }
  154.                                 }
  155.                             }
  156.                         } while (count!=0);
  157.                         if (isDuplicate) groupsNumbersSet.remove(duplicateInt);
  158.                         break outer;
  159.                     }
  160.                     if (i == matrix.length - 1 && j == matrix[i].length - 1) {
  161.                         max--;
  162.                         System.out.println("wtf");
  163.                     }
  164.                 }
  165.             }
  166.             groupsNumber = new ArrayList<>(groupsNumbersSet);
  167.             if (groupsNumber.size()!=0) groupsMap.put(++numberGroup,groupsNumber);
  168.             emptyRowsList.addAll(groupsNumber);
  169.  
  170.  
  171.  
  172.             System.out.println(freeRowsList.size());
  173.             Iterator<Integer> it = freeRowsList.iterator();
  174.             System.out.println("emptyRowsList  =  " + emptyRowsList);
  175.             while (it.hasNext()){
  176.                 Integer x = it.next();
  177.                 for (Integer empty : emptyRowsList) {
  178.                     if (x.equals(empty)){
  179.                         it.remove();
  180.                     }
  181.                 }
  182.             }
  183.  
  184.             if (freeRowsList.size() <= 2) {
  185.                 groupsMap.put(++numberGroup,freeRowsList);
  186.                 break;
  187.             }
  188.  
  189.         } while (max > 0);
  190.         for (Map.Entry<Integer, ArrayList<Integer>> pair : groupsMap.entrySet()) {
  191.             ArrayList<Integer> helpList = pair.getValue();
  192.             result += pair.getKey() + " group : [";
  193.             for (int x : helpList) {
  194.                 result += x + ", "; // 0 == 1 BOT TYT
  195.             }
  196.             result+="]";
  197.             result +="\n";
  198.  
  199.         }
  200.         return result;
  201.     }
  202.  
  203.     public String getNewGroup() {
  204.         ArrayList<String> newCharacterString = new ArrayList<>();
  205.         String result = "";
  206.         for (Map.Entry<Integer, ArrayList<Integer>> pair : groupsMap.entrySet()) {
  207.             String s = "";
  208.             ArrayList<Integer> groupElementsList = pair.getValue();
  209.             for (int x : groupElementsList) {
  210.                 String[] split = list.get(x).split(" ");
  211.                 for (int i = 0; i < split.length; i++) {
  212.                     if (!s.contains(split[i])) s += split[i];
  213.                 }
  214.             }
  215.             newCharacterString.add(s);
  216.             groupToString.put(pair.getKey(), s);
  217.         }
  218.         for (String z : newCharacterString) {
  219.             result += z + "\n";
  220.         }
  221.         return result;
  222.     }
  223.  
  224.     public String checkGroups() {
  225.         ArrayList<String> orderList = new ArrayList<>();
  226.         Map<String, Map<Integer, ArrayList<Integer>>> newGroupMap = new HashMap<>();
  227.         Map<Integer, ArrayList<Integer>> oldGroupMap = new HashMap<>();
  228.         Map<Integer, ArrayList<Integer>> oldGroupMapHelp = new HashMap<>();
  229.         Map<Integer, ArrayList<Integer>> groupElemntMap = new HashMap<>();
  230.         String[] split = getNewGroup().split("\n");
  231.         for (int i = 0; i < split.length; i++) {
  232.             orderList.add(split[i]);
  233.         }
  234.         Collections.sort(orderList, new Comparator<String>() {
  235.             @Override
  236.             public int compare(String s, String t1) {
  237.                 return (t1.length() - s.length());
  238.             }
  239.         });
  240.         for (String st : orderList) {
  241.             oldGroupMap = new HashMap<>();
  242.             for (Map.Entry<Integer, String> pair : groupToString.entrySet()) {
  243.                 for (Map.Entry<Integer, ArrayList<Integer>> oldPair : groupsMap.entrySet()) {
  244.                     if (st.equals(pair.getValue()) && (pair.getKey().equals(oldPair.getKey()))) {
  245.                         oldGroupMap.put(pair.getKey(), oldPair.getValue());
  246.                         oldGroupMapHelp.put(pair.getKey(), oldPair.getValue());
  247.                     }
  248.                 }
  249.             }
  250.             newGroupMap.put(st, oldGroupMap);
  251.         }
  252.         System.out.println(newGroupMap);
  253.         System.out.println(orderList.get(0));
  254.  
  255.         for (Map.Entry<String, Map<Integer, ArrayList<Integer>>> pair : newGroupMap.entrySet()) {
  256.             if (!pair.getKey().equals(orderList.get(0))) {
  257.                 for (Map.Entry<Integer, ArrayList<Integer>> pair2 : pair.getValue().entrySet()) {
  258.                     ArrayList<Integer> checksList = pair2.getValue();
  259.                     Iterator<Integer> it = checksList.iterator();
  260.                     ArrayList<Integer> elementsList = new ArrayList<>();
  261.                     while (it.hasNext()) {
  262.                         Integer next = it.next();
  263.                         String[] sp = list.get(next).split(" ");
  264.                         int spCount = 0;
  265.                         for (int j = 0; j < sp.length; j++) {
  266.                             if (orderList.get(0).contains(sp[j])) spCount++;
  267.                         }
  268.                         if (spCount == sp.length) {
  269.                             elementsList.add(next);
  270.                             it.remove();
  271.                         }
  272.                     }
  273.                     groupElemntMap.put(pair2.getKey(), elementsList);
  274.                     oldGroupMapHelp.put(pair2.getKey(), checksList);
  275.                     //pair.setValue(oldGroupMapHelp);
  276.  
  277.                 }
  278.  
  279.             }
  280.         }
  281.  
  282.         oldGroupMap = newGroupMap.get(orderList.get(0));
  283.         System.out.println(oldGroupMap);
  284.         for (Map.Entry<Integer, ArrayList<Integer>> pair : oldGroupMap.entrySet()) {
  285.             ArrayList<Integer> list = pair.getValue();
  286.             for (Map.Entry<Integer, ArrayList<Integer>> pair2 : groupElemntMap.entrySet()) {
  287.                 ArrayList<Integer> helpList = pair2.getValue();
  288.                 for (int x : helpList) {
  289.                     list.add(x);
  290.                 }
  291.                 System.out.println(pair2.getValue());
  292.                 pair.setValue(list);
  293.                 System.out.println(pair.setValue(list));
  294.             }
  295.         }
  296.         // newGroupMap.put(orderList.get(0),oldGroupMap);
  297.         System.out.println(newGroupMap);
  298.         System.out.println(groupElemntMap);
  299.         System.out.println(orderList);
  300.         String result = "";
  301.         for (int i = 0; i < orderList.size(); i++) {
  302.             for (Map.Entry<String, Map<Integer, ArrayList<Integer>>> pair : newGroupMap.entrySet()) {
  303.                 for (Map.Entry<Integer, ArrayList<Integer>> pair2 : pair.getValue().entrySet()) {
  304.                     if (orderList.get(i).equals(pair.getKey()))
  305.                         result += pair2.getKey() + " group : " + pair2.getValue() + "   " + pair.getKey() + "\n";
  306.                 }
  307.             }
  308.         }
  309.         return result;
  310.     }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement