Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BWT
- {
- class Program
- {
- static void Main(string[] args)
- {
- char[] word;
- Console.WriteLine("Inserisci la parola");
- word = Console.ReadLine().ToCharArray();
- int n = word.Length;
- char[,] m = new char[n, n];
- Fill(word, m, n);
- Console.WriteLine("Permutazioni della parola:");
- Show(m, n);
- Compare(m, n);
- Console.WriteLine("Permutazioni in ordine lessicografico:");
- Show(m, n);
- FindIandL(word, m, n);
- Console.Read();
- }
- //Effettua le permutazioni della parola
- private static void Fill(char[] word, char[,] m, int n)
- {
- int index = n - 1;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- m[i, j] = word[index];
- if (index != n - 1)
- {
- index++;
- }
- else
- {
- index = 0;
- }
- }
- index--;
- }
- Console.WriteLine();
- }
- //Mostra i valori della matrice in console
- private static void Show(char[,] m, int n)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- Console.Write(m[i, j]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- //Trova i valori da ordinare
- private static void Compare(char[,] m, int n)
- {
- int j;
- for (int i = 0; i < n; i++)
- {
- for (j = i + 1; j < n; j++)
- {
- if (m[i, 0].CompareTo(m[j, 0]) == 0)
- {
- Compare2(m, n, i, j);
- }
- else
- {
- if (m[i, 0].CompareTo(m[j, 0]) > 0)
- {
- Sort(i, j, m, n);
- }
- }
- }
- }
- }
- //Trova i valori da ordinare che contengono una o più lettere uguali nella stessa posizione
- private static void Compare2(char[,] m, int n, int i, int j)
- {
- for (int checkCounter = 1; checkCounter < n; checkCounter++)
- {
- if (m[i, checkCounter].CompareTo(m[j, checkCounter]) > 0)
- {
- Sort(i, j, m, n);
- checkCounter = n;
- }
- else if (m[i, checkCounter].CompareTo(m[j, checkCounter]) < 0)
- {
- checkCounter = n;
- }
- }
- }
- //Ordina i valori scelti
- private static void Sort(int k, int j, char[,] m, int n)
- {
- char[] tempM = new char[n];
- for (int i = 0; i < n; i++)
- {
- tempM[i] = m[k, i];
- m[k, i] = m[j, i];
- m[j, i] = tempM[i];
- }
- }
- //Trova i valori di I e L
- private static void FindIandL(char[] word, char[,] m, int n)
- {
- int counter = 0;
- int I = 0;
- char[] L = new char[n];
- char[,] m1 = new char[n, n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (m[i, j].CompareTo(word[j]) == 0)
- {
- counter++;
- }
- if (counter == n - 1)
- {
- I = i;
- }
- }
- counter = 0;
- L[i] = m[i, n - 1];
- }
- Console.WriteLine("I: " + I);
- Console.Write("L: ");
- foreach (char c in L)
- {
- Console.Write(c);
- }
- Console.WriteLine();
- AntiTransform(m, m1, n, I, L);
- }
- //Effettua l'antitrasformata
- private static void AntiTransform(char[,] m, char[,] m1, int n, int I, char[] L)
- {
- int counter = 0;
- int[] T = new int[n];
- char[] word = new char[n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- word[j] = m[i, j];
- }
- ReFill(word, m1, n, counter);
- FindT(m1, m, T, n, counter);
- counter++;
- }
- Console.WriteLine("I valori della matrice m1 sono:");
- Show(m1, n);
- Decode(L, T, n, I);
- }
- //Riempie la matrice m1 con i valori modificati della matrice m
- private static void ReFill(char[] word, char[,] m1, int n, int counter)
- {
- int index = n - 1;
- for (int j = 0; j < n; j++)
- {
- m1[counter, j] = word[index];
- if (index != n - 1)
- {
- index++;
- }
- else
- {
- index = 0;
- }
- }
- }
- //Trova i valori di T
- private static void FindT(char[,] m1, char[,] m, int[] T, int n, int count)
- {
- int counter = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (m1[count, j].CompareTo(m[i, j]) == 0)
- {
- counter++;
- }
- else
- {
- j = n;
- }
- if (counter == n - 1)
- {
- T[count] = i;
- j = n;
- i = n;
- }
- }
- }
- }
- //Trova la parola usando L e T
- private static void Decode(char[] L, int[] T, int n, int I)
- {
- char[] S = new char[n];
- for (int i = n - 1; i > -1; i--)
- {
- S[i] = L[I];
- I = T[I];
- }
- foreach (char c in S)
- {
- Console.Write(c);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement