package com.company; import javafx.util.Pair; import java.util.ArrayList; import java.util.List; public class Matrix { public static void main(String[] args) { // TODO: Эти значения можно ввести с клавиатуры, но я не тестил String key1 = "231"; String key2 = "2-1,3-3,4-2"; int width = 3; int height = 4; String src = "Hello world!"; String encodedString = encode(src, key1, key2, width, height); System.out.println(encodedString); String decodedString = decode(encodedString, key1, key2, width, height); System.out.println(decodedString); } private static String encode(String src, String key1, String key2, int width, int height) { char[][][] matrices = writeEncodeMatrices(src, key2, width, height); StringBuilder dst = new StringBuilder(); for (char[][] matrix : matrices) { for (int keyColumn = 0; keyColumn < key1.length(); keyColumn++) { int columnIndex = Integer.parseInt(Character.toString(key1.charAt(keyColumn))) - 1; for (int row = 0; row < height; row++) { char value = matrix[row][columnIndex]; if (value != '\0' && value != '*') dst.append(value); } } } return dst.toString(); } private static String decode(String src, String key1, String key2, int width, int height) { char[][][] matrices = writeDecodeMatrices(src, key1, key2, width, height); for (char[][] matrix : matrices) { printMatrix(matrix); } StringBuilder dst = new StringBuilder(); for (char[][] matrix : matrices) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { char c = matrix[i][j]; if (c != '\0' && c != '*') { dst.append(c); } } } } return dst.toString(); } private static char[][][] writeDecodeMatrices(String src, String key1, String key2, int width, int height) { List> pairs = parseKey2(key2); List matricesList = new ArrayList<>(); int srcOffset = 0; while (srcOffset < src.length()) { char[][] chars = new char[height][width]; int writeRows = height; int left = src.length() - srcOffset; if (left < width * height) { writeRows = left / width; if (left % width != 0) { writeRows++; } } for (int keyColumn = 0; keyColumn < key1.length(); keyColumn++) { int col = Integer.parseInt(Character.toString(key1.charAt(keyColumn))) - 1; for (int row = 0; row < height; row++) { int finalRow = row; if (pairs.stream().anyMatch(pair -> pair.getKey() == finalRow && pair.getValue() == col)) { chars[row][col] = '*'; } else { if (srcOffset == src.length()) { chars[row][col] = '\0'; } else { if (row < writeRows) { chars[row][col] = src.charAt(srcOffset); srcOffset++; } } } } } matricesList.add(chars); } char[][][] matrices = new char[matricesList.size()][height][width]; for (int i = 0; i < matricesList.size(); i++) { matrices[i] = matricesList.get(i); } return matrices; } private static char[][][] writeEncodeMatrices(String src, String key2, int width, int height) { List> pairs = parseKey2(key2); List matricesList = new ArrayList<>(); int srcOffset = 0; while (srcOffset < src.length()) { char[][] chars = new char[height][width]; for (int indexInMatrix = 0; indexInMatrix < width * height; indexInMatrix++) { int row = indexInMatrix / width; int col = indexInMatrix % width; if (pairs.stream().anyMatch(pair -> pair.getKey() == row && pair.getValue() == col)) { chars[row][col] = '*'; } else { if (srcOffset == src.length()) { chars[row][col] = '\0'; } else { chars[row][col] = src.charAt(srcOffset); srcOffset++; } } } matricesList.add(chars); } char[][][] matrices = new char[matricesList.size()][height][width]; for (int i = 0; i < matricesList.size(); i++) { matrices[i] = matricesList.get(i); } return matrices; } private static List> parseKey2(String key2) { List> list = new ArrayList<>(); String[] parts = key2.split(","); for (String part : parts) { String[] indexesStrings = part.split("-"); int row = Integer.parseInt(indexesStrings[0]) - 1; int column = Integer.parseInt(indexesStrings[1]) - 1; list.add(new Pair<>(row, column)); } return list; } public static void printMatrix(char[][] matrix) { for (int i = 0; i < matrix[0].length * 4 + 1; i++) { System.out.print('-'); } System.out.println(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print("| " + (matrix[i][j] == '\0' ? " " : matrix[i][j]) + " "); } System.out.println("|"); for (int j = 0; j < matrix[0].length * 4 + 1; j++) { System.out.print('-'); } System.out.println(); } System.out.println(); } }