Edit distance algorithm (C # Version)

Source: Internet
Author: User

Port Based on Java version

View code

 1   ///   <Summary>  2       ///  Edit distanceAlgorithm  3       ///   </Summary>  4       Public   Class  Editdistance  5  {  6           /*  *  7   * Minimum number of three numbers: Mar 1, 2007  8   *  9   * @ Param  10   * @ Param B  11   * @ Param C  12   * @ Return  13      */  14           Private   Static   Int Minimum ( Int A, Int B, Int  C)  15   {  16               Int  Mi;  17   18 Mi =A;  19               If (B < Mi)  20   {  21 Mi = B;  22   }  23               If (C < Mi)  24   {  25 Mi = C;  26   }  27               Return  Mi;  28   }  29   30           /*  *  31   * Calculate the editing distance between two strings, which is Mar 1, 2007.  32   * @ Param s 33   * @ Param t  34   * @ Return  35            */  36           Public   Static   Int  Geteditdistance (string S, string T)  37   {  38               Int [,] D; // Matrix  39               Int N = 0 ; //  Length of S  40               Int M = 0 ; //  Length of T  41               Int I; //  Iterates through S  42              Int J; //  Iterates through T  43               Char S_ I; //  Ith character of S  44               Char T_j; //  Jth character of T  45               Int Cost; //  Cost  46  47               //  Step 1  48   49 N = S. length;  50 M = T. length;  51               If (N = 0  )  52   {  53                   Return M;  54   }  55               If (M = 0  )  56   {  57                   Return  N;  58   }  59 D = New  Int [N + 1 , M + 1  ];  60               //  D = new int [n + 1] [+ 1];  61   62               //  Step 2  63   64               For (I = 0 ; I <= N; I ++ ) 65   {  66 D [I, 0 ] = 1  ;  67   }  68   69               For (J = 0 ; J <= m; j ++ )  70   {  71 D [ 0 , J] = J;  72   }  73   74               //  Step 3  75   76               For (I = 1 ; I <= N; I ++ )  77   {  78 S_ I = s [I- 1  ];  79                   //  Step 4  80                   For (J = 1 ; J <= m; j ++ )  81   {  82 T_j = T [J- 1  ];  83                      //  Step 5  84                       If (S_ I = T_j)  85   {  86 Cost = 0  ;  87   }  88                       Else  89   { 90 Cost = 1  ;  91   }  92                       //  Step 6  93 D [I, j] = Minimum (d [I- 1 , J] + 1 , D [I, j- 1 ] + 1  ,  94 D [I- 1 , J- 1 ] + Cost );  95   }  96   }  97               //  Step 7  98               Return  D [n, m];  99   100   } 101   102   103 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.