Advertisement
TheGoldsmith

BWT

Oct 26th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BWT
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {      
  13.             char[] word;
  14.  
  15.             Console.WriteLine("Inserisci la parola");
  16.             word = Console.ReadLine().ToCharArray();
  17.  
  18.             int n = word.Length;
  19.             char[,] m = new char[n, n];
  20.  
  21.             Fill(word, m, n);
  22.  
  23.             Console.WriteLine("Permutazioni della parola:");
  24.  
  25.             Show(m, n);
  26.  
  27.             Compare(m, n);
  28.            
  29.             Console.WriteLine("Permutazioni in ordine lessicografico:");        
  30.             Show(m, n);
  31.  
  32.             FindIandL(word, m, n);
  33.  
  34.             Console.Read();
  35.         }
  36.  
  37.         //Effettua le permutazioni della parola
  38.         private static void Fill(char[] word, char[,] m, int n)
  39.         {  
  40.             int index = n - 1;
  41.  
  42.             for (int i = 0; i < n; i++)
  43.             {
  44.                 for (int j = 0; j < n; j++)
  45.                 {
  46.                     m[i, j] = word[index];
  47.  
  48.                     if (index != n - 1)
  49.                     {
  50.                         index++;
  51.                     }
  52.                     else
  53.                     {
  54.                         index = 0;
  55.                     }
  56.                 }
  57.                 index--;              
  58.             }
  59.             Console.WriteLine();
  60.         }
  61.  
  62.         //Mostra i valori della matrice in console
  63.         private static void Show(char[,] m, int n)
  64.         {
  65.             for (int i = 0; i < n; i++)
  66.             {
  67.                 for (int j = 0; j < n; j++)
  68.                 {
  69.                     Console.Write(m[i, j]);
  70.                 }
  71.                 Console.WriteLine();
  72.             }
  73.             Console.WriteLine();
  74.         }
  75.  
  76.         //Trova i valori da ordinare
  77.         private static void Compare(char[,] m, int n)
  78.         {
  79.             int j;
  80.             for (int i = 0; i < n; i++)
  81.             {
  82.                 for (j = i + 1; j < n; j++)
  83.                 {              
  84.                     if (m[i, 0].CompareTo(m[j, 0]) == 0)
  85.                     {
  86.                         Compare2(m, n, i, j);                      
  87.                     }
  88.                     else
  89.                     {
  90.                         if (m[i, 0].CompareTo(m[j, 0]) > 0)
  91.                         {
  92.                             Sort(i, j, m, n);
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.  
  99.         //Trova i valori da ordinare che contengono una o più lettere uguali nella stessa posizione
  100.         private static void Compare2(char[,] m, int n, int i, int j)
  101.         {
  102.             for (int checkCounter = 1; checkCounter < n; checkCounter++)
  103.             {
  104.                 if (m[i, checkCounter].CompareTo(m[j, checkCounter]) > 0)
  105.                 {
  106.                     Sort(i, j, m, n);
  107.                     checkCounter = n;
  108.                 }
  109.                 else if (m[i, checkCounter].CompareTo(m[j, checkCounter]) < 0)
  110.                 {
  111.                     checkCounter = n;
  112.                 }
  113.             }
  114.         }
  115.  
  116.         //Ordina i valori scelti
  117.         private static void Sort(int k, int j, char[,] m, int n)
  118.         {
  119.             char[] tempM = new char[n];
  120.  
  121.             for (int i = 0; i < n; i++)
  122.             {
  123.                 tempM[i] = m[k, i];
  124.                 m[k, i] = m[j, i];
  125.                 m[j, i] = tempM[i];
  126.             }
  127.         }
  128.        
  129.         //Trova i valori di I e L
  130.         private static void FindIandL(char[] word, char[,] m, int n)
  131.         {
  132.             int counter = 0;
  133.             int I = 0;
  134.             char[] L = new char[n];
  135.             char[,] m1 = new char[n, n];
  136.  
  137.             for (int i = 0; i < n; i++)
  138.             {
  139.                 for (int j = 0; j < n; j++)
  140.                 {
  141.                     if (m[i, j].CompareTo(word[j]) == 0)
  142.                     {
  143.                         counter++;
  144.                     }
  145.                     if (counter == n - 1)
  146.                     {
  147.                         I = i;
  148.                     }
  149.                 }
  150.                 counter = 0;
  151.                 L[i] = m[i, n - 1];
  152.             }
  153.  
  154.             Console.WriteLine("I: " + I);
  155.  
  156.             Console.Write("L: ");
  157.             foreach (char c in L)
  158.             {
  159.                 Console.Write(c);
  160.             }
  161.  
  162.             Console.WriteLine();
  163.  
  164.             AntiTransform(m, m1, n, I, L);
  165.         }
  166.  
  167.         //Effettua l'antitrasformata
  168.         private static void AntiTransform(char[,] m, char[,] m1, int n, int I, char[] L)
  169.         {
  170.             int counter = 0;
  171.             int[] T = new int[n];
  172.             char[] word = new char[n];
  173.  
  174.             for (int i = 0; i < n; i++)
  175.             {
  176.                 for (int j = 0; j < n; j++)
  177.                 {
  178.                     word[j] = m[i, j];
  179.                 }
  180.  
  181.                 ReFill(word, m1, n, counter);
  182.  
  183.                 FindT(m1, m, T, n, counter);
  184.  
  185.                 counter++;
  186.             }
  187.             Console.WriteLine("I valori della matrice m1 sono:");
  188.             Show(m1, n);
  189.  
  190.             Decode(L, T, n, I);
  191.         }
  192.  
  193.         //Riempie la matrice m1 con i valori modificati della matrice m
  194.         private static void ReFill(char[] word, char[,] m1, int n, int counter)
  195.         {
  196.             int index = n - 1;
  197.  
  198.             for (int j = 0; j < n; j++)
  199.             {
  200.                 m1[counter, j] = word[index];
  201.  
  202.                 if (index != n - 1)
  203.                 {
  204.                     index++;
  205.                 }
  206.                 else
  207.                 {
  208.                     index = 0;
  209.                 }
  210.             }
  211.         }
  212.  
  213.         //Trova i valori di T
  214.         private static void FindT(char[,] m1, char[,] m, int[] T, int n, int count)
  215.         {
  216.             int counter = 0;
  217.  
  218.             for (int i = 0; i < n; i++)
  219.             {
  220.                 for (int j = 0; j < n; j++)
  221.                 {
  222.                     if (m1[count, j].CompareTo(m[i, j]) == 0)
  223.                     {
  224.                         counter++;
  225.                     }
  226.                     else
  227.                     {
  228.                         j = n;
  229.                     }
  230.                     if (counter == n - 1)
  231.                     {
  232.                         T[count] = i;
  233.                         j = n;
  234.                         i = n;
  235.                     }
  236.                 }
  237.             }
  238.         }
  239.  
  240.         //Trova la parola usando L e T
  241.         private static void Decode(char[] L, int[] T, int n, int I)
  242.         {
  243.             char[] S = new char[n];
  244.             for (int i = n - 1; i > -1; i--)
  245.             {
  246.                 S[i] = L[I];
  247.                 I = T[I];
  248.             }
  249.             foreach (char c in S)
  250.             {
  251.                 Console.Write(c);
  252.             }
  253.         }
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement