Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Collections.Generic;
- 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 str)
- {
- Regex regF = new Regex(@"[^a-z0-9\s]");
- str = regF.Replace(str.ToLower(), string.Empty);
- MatchCollection regS = Regex.Matches(str, @"[a-z0-9]{4,}\s?");
- str = "";
- foreach (Match match in regS)
- {
- str += match.Value;
- }
- return str;
- }
- 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 = 0;
- if (secondString[j-1] == firstString[i-1])
- toReplace = 0;
- else toReplace = 1;
- mass[i, j] = Minimum(mass[i-1, j] + 1, mass[i,j-1] + 1, mass[i-1, j-1] + toReplace);
- }
- 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(Levenstain(firstString, secondString));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement