Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Matrix {
- private int[][] matrix;
- private ArrayList<String> list;
- private String[] charac;
- private int[][] elements;
- private Map<Integer, ArrayList<Integer>> groupsMap = new HashMap<>();
- private Map<Integer, String> groupToString = new HashMap<>();
- public void setList(ArrayList<String> list) {
- this.list = list;
- }
- public ArrayList<String> getList() {
- return list;
- }
- private String getCharacString() {
- String summ = "";
- for (String s : list) {
- summ += s + " ";
- }
- return summ.trim();
- }
- public String getCharacterString() { // proverka
- String[] split = getCharacString().split(" ");
- String s = split[0];
- for (int i = 1; i < split.length; i++) {
- if (!s.contains(split[i])) s += " " + split[i];
- }
- return s;
- }
- public void fillCharacElements() { // 1 : 0
- int stringLength = getCharacterString().split(" ").length;
- String[] split = getCharacterString().split(" ");
- elements = new int[list.size()][stringLength];
- for (int i = 0; i < list.size(); i++) {
- for (int j = 0; j < stringLength; j++) {
- if (list.get(i).contains(split[j])) {
- elements[i][j] = 1;
- } else elements[i][j] = 0;
- }
- }
- System.out.println(getCharacterString());
- for (int i = 0; i < list.size(); i++) {
- for (int j = 0; j < stringLength; j++) {
- System.out.print(elements[i][j]);
- }
- System.out.println();
- }
- }
- public String elementsCastToString() {
- String s = "";
- for (int i = 0; i < elements.length; i++) {
- for (int j = 0; j < elements[i].length; j++) {
- if (j % 3 == 0) s += elements[i][j] + " ";
- else s += elements[i][j] + " ";
- }
- s += "\n";
- }
- return s.trim();
- }
- public String getMatrix() {
- String s = "";
- matrix = new int[list.size()][list.size()];
- int summ = 0;
- for (int i = 0; i < elements.length - 1; i++) {
- int k = 1;
- if (k == elements.length) break;
- while (true) {
- for (int j = 0; j < elements[i].length; j++) {
- if (elements[i][j] == elements[i + k][j]) summ++;
- }
- matrix[k + i][i] = summ;
- matrix[i][k + i] = summ;
- summ = 0;
- k++;
- if (i + k == elements.length) break;
- }
- }
- for (int i = 0; i < matrix.length; i++) {
- matrix[i][i] = -1;
- }
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- if (j % 3 == 0) s += matrix[i][j] + " ";
- else s += matrix[i][j] + " ";
- }
- s += "\n";
- }
- return s.trim();
- }
- public String getGroup() {
- Set<Integer> emptyRowsList = new LinkedHashSet<>();
- ArrayList<Integer> freeRowsList = new ArrayList<>();
- int numberGroup = 0;
- int rowsCount = 0;
- int max = matrix[0][0];
- String result = "";
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- if (matrix[i][j] > max) max = matrix[i][j];
- }
- }
- for (int i = 0; i < matrix.length; i++) {
- freeRowsList.add(i);
- }
- do {
- ArrayList<Integer> groupsNumber = new ArrayList<>();
- Set<Integer> groupsNumbersSet = new LinkedHashSet<>();
- outer:
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- if (matrix[i][j] == max && (freeRowsList.contains(i) || freeRowsList.contains(j))) {
- boolean isDuplicate = false;
- int duplicateInt = 0;
- groupsNumbersSet.add(i);
- groupsNumbersSet.add(j);
- int count;
- do {
- count = 0;
- groupsNumber = new ArrayList<>(groupsNumbersSet);
- for (int k : groupsNumber) {
- for (int l = 0; l < matrix[k].length; l++) {
- if (emptyRowsList.contains(l)) {
- isDuplicate = true;
- duplicateInt = l;
- }
- if (max == matrix[k][l] ) {
- int size = groupsNumbersSet.size();
- groupsNumbersSet.add(l);
- int currentSize = groupsNumbersSet.size();
- if (currentSize>size) count++;
- }
- }
- }
- } while (count!=0);
- if (isDuplicate) groupsNumbersSet.remove(duplicateInt);
- break outer;
- }
- if (i == matrix.length - 1 && j == matrix[i].length - 1) {
- max--;
- System.out.println("wtf");
- }
- }
- }
- groupsNumber = new ArrayList<>(groupsNumbersSet);
- if (groupsNumber.size()!=0) groupsMap.put(++numberGroup,groupsNumber);
- emptyRowsList.addAll(groupsNumber);
- System.out.println(freeRowsList.size());
- Iterator<Integer> it = freeRowsList.iterator();
- System.out.println("emptyRowsList = " + emptyRowsList);
- while (it.hasNext()){
- Integer x = it.next();
- for (Integer empty : emptyRowsList) {
- if (x.equals(empty)){
- it.remove();
- }
- }
- }
- if (freeRowsList.size() <= 2) {
- groupsMap.put(++numberGroup,freeRowsList);
- break;
- }
- } while (max > 0);
- for (Map.Entry<Integer, ArrayList<Integer>> pair : groupsMap.entrySet()) {
- ArrayList<Integer> helpList = pair.getValue();
- result += pair.getKey() + " group : [";
- for (int x : helpList) {
- result += x + ", "; // 0 == 1 BOT TYT
- }
- result+="]";
- result +="\n";
- }
- return result;
- }
- public String getNewGroup() {
- ArrayList<String> newCharacterString = new ArrayList<>();
- String result = "";
- for (Map.Entry<Integer, ArrayList<Integer>> pair : groupsMap.entrySet()) {
- String s = "";
- ArrayList<Integer> groupElementsList = pair.getValue();
- for (int x : groupElementsList) {
- String[] split = list.get(x).split(" ");
- for (int i = 0; i < split.length; i++) {
- if (!s.contains(split[i])) s += split[i];
- }
- }
- newCharacterString.add(s);
- groupToString.put(pair.getKey(), s);
- }
- for (String z : newCharacterString) {
- result += z + "\n";
- }
- return result;
- }
- public String checkGroups() {
- ArrayList<String> orderList = new ArrayList<>();
- Map<String, Map<Integer, ArrayList<Integer>>> newGroupMap = new HashMap<>();
- Map<Integer, ArrayList<Integer>> oldGroupMap = new HashMap<>();
- Map<Integer, ArrayList<Integer>> oldGroupMapHelp = new HashMap<>();
- Map<Integer, ArrayList<Integer>> groupElemntMap = new HashMap<>();
- String[] split = getNewGroup().split("\n");
- for (int i = 0; i < split.length; i++) {
- orderList.add(split[i]);
- }
- Collections.sort(orderList, new Comparator<String>() {
- @Override
- public int compare(String s, String t1) {
- return (t1.length() - s.length());
- }
- });
- for (String st : orderList) {
- oldGroupMap = new HashMap<>();
- for (Map.Entry<Integer, String> pair : groupToString.entrySet()) {
- for (Map.Entry<Integer, ArrayList<Integer>> oldPair : groupsMap.entrySet()) {
- if (st.equals(pair.getValue()) && (pair.getKey().equals(oldPair.getKey()))) {
- oldGroupMap.put(pair.getKey(), oldPair.getValue());
- oldGroupMapHelp.put(pair.getKey(), oldPair.getValue());
- }
- }
- }
- newGroupMap.put(st, oldGroupMap);
- }
- System.out.println(newGroupMap);
- System.out.println(orderList.get(0));
- for (Map.Entry<String, Map<Integer, ArrayList<Integer>>> pair : newGroupMap.entrySet()) {
- if (!pair.getKey().equals(orderList.get(0))) {
- for (Map.Entry<Integer, ArrayList<Integer>> pair2 : pair.getValue().entrySet()) {
- ArrayList<Integer> checksList = pair2.getValue();
- Iterator<Integer> it = checksList.iterator();
- ArrayList<Integer> elementsList = new ArrayList<>();
- while (it.hasNext()) {
- Integer next = it.next();
- String[] sp = list.get(next).split(" ");
- int spCount = 0;
- for (int j = 0; j < sp.length; j++) {
- if (orderList.get(0).contains(sp[j])) spCount++;
- }
- if (spCount == sp.length) {
- elementsList.add(next);
- it.remove();
- }
- }
- groupElemntMap.put(pair2.getKey(), elementsList);
- oldGroupMapHelp.put(pair2.getKey(), checksList);
- //pair.setValue(oldGroupMapHelp);
- }
- }
- }
- oldGroupMap = newGroupMap.get(orderList.get(0));
- System.out.println(oldGroupMap);
- for (Map.Entry<Integer, ArrayList<Integer>> pair : oldGroupMap.entrySet()) {
- ArrayList<Integer> list = pair.getValue();
- for (Map.Entry<Integer, ArrayList<Integer>> pair2 : groupElemntMap.entrySet()) {
- ArrayList<Integer> helpList = pair2.getValue();
- for (int x : helpList) {
- list.add(x);
- }
- System.out.println(pair2.getValue());
- pair.setValue(list);
- System.out.println(pair.setValue(list));
- }
- }
- // newGroupMap.put(orderList.get(0),oldGroupMap);
- System.out.println(newGroupMap);
- System.out.println(groupElemntMap);
- System.out.println(orderList);
- String result = "";
- for (int i = 0; i < orderList.size(); i++) {
- for (Map.Entry<String, Map<Integer, ArrayList<Integer>>> pair : newGroupMap.entrySet()) {
- for (Map.Entry<Integer, ArrayList<Integer>> pair2 : pair.getValue().entrySet()) {
- if (orderList.get(i).equals(pair.getKey()))
- result += pair2.getKey() + " group : " + pair2.getValue() + " " + pair.getKey() + "\n";
- }
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement