Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- class MainClass
- {
- static int Minimum(int a, int b, int c)
- {
- int min = 0;
- if (a <= b && a <= c)
- min = a;
- else if (b <= a && b <= c)
- min = b;
- else if (c <= a && c <= b)
- min = c;
- return min;
- }
- public static string Replace_s(string text)
- {
- string pattern = @"[A-Z]";
- string to_delete = @"[^a-z0-9 \s]";
- string to_delete_word = @"(\b([a-z0-9]){1,3}\b)";
- string to_delete_space = @"(\s){2,}";
- string to_replace = @" ";
- string first_space = @"^\s+";
- string last_space = @"\s+$";
- text = Regex.Replace(text, pattern, m => m.ToString().ToLower());
- text = Regex.Replace(text, to_delete, String.Empty);
- text = Regex.Replace(text, to_delete_word, String.Empty);
- text = Regex.Replace(text, to_delete_space, to_replace);
- text = Regex.Replace(text, last_space, String.Empty);
- text = Regex.Replace(text, first_space, String.Empty);
- return text;
- }
- public static int Levenstain(string firstString, string secondString)
- {
- int result;
- int n = firstString.Length + 1;
- int m = secondString.Length + 1;
- int [,] mass = new int[n,m];
- for(int i = 0; i < n; i++)
- mass[i,0] = i;
- for(int j = 0; j < m; j++)
- mass[0,j] = j;
- for(int i = 1; i < n; i++)
- for (int j = 1; j < m; j++)
- {
- int toReplace = firstString[i - 1] == secondString[j - 1] ? 0 : 1;
- mass[i, j] = Minimum(mass[i-1, j] + 1, mass[i,j-1] + 1, mass[i-1, j-1] + toReplace);
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- Console.Write($"{mass[i,j]} ");
- }
- Console.WriteLine();
- }
- result = mass[n-1,m-1];
- return result;
- }
- public static void Main(string[] args)
- {
- string firstString;
- string secondString;
- firstString = Console.ReadLine();
- secondString = Console.ReadLine();
- firstString = Replace_s(firstString);
- secondString = Replace_s(secondString);
- Console.WriteLine(firstString);
- Console.WriteLine(secondString);
- Console.WriteLine(Levenstain(firstString, secondString));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement