Advertisement
allia

gft

Mar 2nd, 2021
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. class MainClass
  6. {
  7.   static int Minimum(int a, int b, int c)
  8.  {
  9.    int min = 0;
  10.  
  11.    if (a <= b && a <= c)
  12.     min = a;
  13.    else if (b <= a && b <= c)
  14.     min = b;
  15.    else if (c <= a && c <= b)
  16.     min = c;
  17.    return min;
  18.  }
  19.  
  20.   public static string Replace_s(string text)
  21.   {
  22.     string pattern = @"[A-Z]";
  23.     string to_delete = @"[^a-z0-9 \s]";
  24.     string to_delete_word = @"(\b([a-z0-9]){1,3}\b)";
  25.     string to_delete_space = @"(\s){2,}";
  26.     string to_replace = @" ";
  27.     string first_space = @"^\s+";
  28.     string last_space = @"\s+$";
  29.  
  30.       text = Regex.Replace(text, pattern, m => m.ToString().ToLower());
  31.       text = Regex.Replace(text, to_delete, String.Empty);
  32.       text = Regex.Replace(text, to_delete_word, String.Empty);
  33.       text = Regex.Replace(text, to_delete_space, to_replace);
  34.       text = Regex.Replace(text, last_space, String.Empty);
  35.       text = Regex.Replace(text, first_space, String.Empty);
  36.     return text;
  37.   }
  38.  
  39.   public static int Levenstain(string firstString, string secondString)
  40.   {
  41.    int result;
  42.    int n = firstString.Length + 1;
  43.    int m = secondString.Length + 1;
  44.    int [,] mass = new int[n,m];
  45.  
  46.    for(int i = 0; i < n; i++)
  47.     mass[i,0] = i;
  48.  
  49.   for(int j = 0; j < m; j++)
  50.    mass[0,j] = j;
  51.  
  52.   for(int i = 1; i < n; i++)
  53.      for (int j = 1; j < m; j++)
  54.      {
  55.       int toReplace = firstString[i - 1] == secondString[j - 1] ? 0 : 1;
  56.       mass[i, j] = Minimum(mass[i-1, j] + 1, mass[i,j-1] + 1, mass[i-1, j-1] + toReplace);
  57.      }
  58.    
  59.    for (int i = 0; i < n; i++)
  60.       {
  61.         for (int j = 0; j < m; j++)
  62.           {
  63.             Console.Write($"{mass[i,j]}  ");
  64.           }
  65.         Console.WriteLine();
  66.       }
  67.  
  68.    result = mass[n-1,m-1];
  69.    return result;
  70.   }
  71.   public static void Main(string[] args)
  72.   {
  73.     string firstString;
  74.     string secondString;
  75.  
  76.     firstString = Console.ReadLine();
  77.     secondString = Console.ReadLine();
  78.      firstString = Replace_s(firstString);
  79.      secondString = Replace_s(secondString);
  80.     Console.WriteLine(firstString);
  81.     Console.WriteLine(secondString);
  82.     Console.WriteLine(Levenstain(firstString, secondString));
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement