计算字符串相识度

最近在做这方面的应用,把我找到的资料贴出来,有需要的人可以参考参考。
1.编辑距离(Levenshtein Distance)
编辑距离就是用来计算从原串(s)转换到目标串(t)所需要的最少的插入,删除和替换
的数目,在NLP中应用比较广泛,如一些评测方法中就用到了(wer,mWer等),同时也常用来计算你对原文本所作的改动数。编辑距离的算法是首先由俄国科学家Levenshtein提出的,故又叫Levenshtein Distance。
Levenshtein Distance算法可以看作动态规划。它的思路就是从两个字符串的左边开始比较,记录已经比较过的子串相似度(实际上叫做距离),然后进一步得到下一个字符位置时的相似度。 用下面的例子: GUMBO和GAMBOL。当算到矩阵D[3,3]位置时,也就是当比较到GUM和GAM时,要从已经比较过的3对子串GU-GAM, GUM-GA和GU-GA之中选一个差别最小的来当它的值. 所以要从左上到右下构造矩阵。
编辑距离的伪算法:
整数 Levenshtein距离(字符 str1[1..lenStr1], 字符 str2[1..lenStr2])
   宣告 int d[0..lenStr1, 0..lenStr2]
   宣告 int i, j, cost
 
   对于 i 等于 由 0 至 lenStr1
       d[i, 0] := i
   对于 j 等于 由 0 至 lenStr2
       d[0, j] := j
   对于 i 等于 由 1 至 lenStr1
       对于 j 等于 由 1 至 lenStr2
           若 str1[i] = str2[j] 则 cost := 0
                                否则 cost := 1
           d[i, j] := 最小值(
                                d[i-1, j  ] + 1,     // 删除
                                d[i  , j-1] + 1,     // 插入
                                d[i-1, j-1] + cost   // 替换
                            )
返回 d[lenStr1, lenStr2]

double Minimum(double a, double b, double c) 

 double mi; 
 
 mi = a; 
 if (b < mi) { 
  mi = b; 
 } 
 if (c < mi) { 
  mi = c; 
 } 
 return mi; 
}


int* GetCellPointer(int *pOrigin, int col, int row, int nCols) 

 return pOrigin + col + (row * (nCols + 1)); 
}

int GetAt(int *pOrigin, int col, int row, int nCols) 

 int *pCell; 
 
 pCell = GetCellPointer (pOrigin, col, row, nCols); 
 return *pCell; 
}

void PutAt(int *pOrigin, int col, int row, int nCols, double x) 

 int *pCell; 
 pCell = GetCellPointer (pOrigin, col, row, nCols); 
 *pCell = x; 
}

//编辑距离
LD(const char *s, const char *t)
{
 int *d; // pointer to matrix
 int n; // length of s
 int m; // length of t
 int i; // iterates through s
 int j; // iterates through t
 char s_i1; // ith character of s
 char s_i2; // ith character of s
 char t_j1; // jth character of t
 char t_j2; // jth character of t
 int *cost; // cost代价矩阵
 int result; // result
 int cell; // contents of target cell
 int above; // contents of cell immediately above
 int left; // contents of cell immediately to left
 int diag; // contents of cell immediately above and to left
 int sz; // number of cells in matrix

 // Step 1

 n = strlen (s);
 m = strlen (t);
 if (n == 0) 
 {
  return m;
 }
 if (m == 0) 
 {
  return n;
 }
 sz = (n+1) * (m+1) * sizeof (int);
 d = (int *) malloc (sz);
 cost = (int *) malloc (sz);

 // Step 2

 for (i = 0; i <= n; i++) 
 {
  PutAt (d, i, 0, n, i);
 }

 for (j = 0; j <= m; j++)
 {
  PutAt (d, 0, j, n, j);
 }
 for (int g=0;g<=m;g++)//把代价距离矩阵全部初始化为同一个值,以后可根据此值判断相应的方格是否被赋过值
 {
  for(int h=0;h<=n;h++)
  {
   PutAt(cost,h,g,n,2);
  }
 }
 // Step 3

 for (i = 1; i <= n; i++) 
 {

  s_i1 = s[i-1];
  s_i2 = s[i];
  bool sbd=false;
  bool tbd=false;
  if(s_i1>=' '&&s_i1<='@'||s_i1>='A'&&s_i1<='~')
  {//s为标点符号或其他非中文符号和数字
  sbd=true;
  }
  // Step 4

  for (j = 1; j <= m; j++) 
  {

   tbd=false;
   t_j1 = t[j-1];
   t_j2 = t[j];
   // Step 5
   if(t_j1>=' '&&t_j1<='@'||t_j1>='A'&&t_j1<='~')
   {//t也为标点符号
    tbd=true;
   }
   if(!sbd)
   {//s为汉字
    if(!tbd)
    {//t也为汉字
     if (s_i1 == t_j1&&s_i2 == t_j2) 
     {
      bool tt=false;
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,0);
       tt=true;
      }
      if(tt)
      {//因为st全市汉字,所以把代价矩阵他相邻的未赋过值的三个格赋值
       int temp1=GetAt(cost,i+1,j,n);
       if(temp1==2)
       {
        PutAt(cost,i+1,j,n,0);
       }
       int temp2=GetAt(cost,i,j+1,n);
       if(temp2==2)
       {
        PutAt(cost,i,j+1,n,0);
       }
       int temp3=GetAt(cost,i+1,j+1,n);
       if(temp3==2)
       {
        PutAt(cost,i+1,j+1,n,0);
       }
      }
     }
     else 
     {
      bool tt=false;
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,1);
       tt=true;
      }
      if(tt)
      {
       int temp1=GetAt(cost,i+1,j,n);
       if(temp1==2)
       {
        PutAt(cost,i+1,j,n,1);
       }
       int temp2=GetAt(cost,i,j+1,n);
       if(temp2==2)
       {
        PutAt(cost,i,j+1,n,1);
       }
       int temp3=GetAt(cost,i+1,j+1,n);
       if(temp3==2)
       {
        PutAt(cost,i+1,j+1,n,1);
       }
      }
     }
    }
    else
    {//t为符号
     bool tt=false;
     int temp=GetAt(cost,i,j,n);
     if(temp==2)
     {
      PutAt(cost,i,j,n,1);
      tt=true;
     }
     if(tt)
     {
      int temp1=GetAt(cost,i+1,j,n);
      if(temp1==2)
      {
       PutAt(cost,i+1,j,n,1);
      }
     }
    
    }

   }
   else
   {//s为符号
    if(!tbd)
    {//t为汉字 
     bool tt=false;
     int temp=GetAt(cost,i,j,n);
     if(temp==2)
     {
      PutAt(cost,i,j,n,1);
      tt=true;
     }
     if(tt)
     {
      int temp1=GetAt(cost,i,j+1,n);
      if(temp1==2)
      {
       PutAt(cost,i,j+1,n,1);
      }
     }
    }
    else
    {
     if(s_i1==t_j1)
     {
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,0);
      }
     }
     else
     {
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,1);
      }
     }
    }

   }

    // Step 6

   above = GetAt (d,i-1,j, n);
   left = GetAt (d,i, j-1, n);
   diag = GetAt (d, i-1,j-1, n);
   int curcost=GetAt(cost,i,j,n);
   cell = Minimum (above + 1, left + 1, diag + curcost);
   PutAt (d, i, j, n, cell);
  }
 }

  // Step 7

  result = GetAt (d, n, m, n);
  free (d);
  return result;

}

2.最长公共子串 (LCS)
LCS问题就是求两个字符串最长公共子串的问题。解法就是用一个矩阵来记录两个字符
串中所有位置的两个字符之间的匹配情况,若是匹配则为1,否则为0。然后求出对角线最长的1序列,其对应的位置就是最长匹配子串的位置.
下面是字符串21232523311324和字符串312123223445的匹配矩阵,前者为X方向的,
后者为Y方向的。不难找到,红色部分是最长的匹配子串。通过查找位置我们得到最长的匹配子串为:21232
    0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
但是在0和1的矩阵中找最长的1对角线序列又要花去一定的时间。通过改进矩阵的生成方式和设置标记变量,可以省去这部分时间。下面是新的矩阵生成方式:
    0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  0 1 0 0 0 0 0 0 0 2 1 0 0 0 0
  1 0 2 0 1 0 1 0 0 0 0 0 1 0 0
  0 2 0 0 0 0 0 0 0 1 1 0 0 0 0
  1 0 3 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 4 0 0 0 2 1 0 0 1 0 0 0
  1 0 1 0 5 0 1 0 0 0 0 0 2 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 2 0 0 0 2 1 0 0 1 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
当字符匹配的时候,我们并不是简单的给相应元素赋上1,而是赋上其左上角元素的值加一。我们用两个标记变量来标记矩阵中值最大的元素的位置,在矩阵生成的过程中来判断当前生成的元素的值是不是最大的,据此来改变标记变量的值,那么到矩阵完成的时候,最长匹配子串的位置和长度就已经出来了。

//最长公共子串
char* LCS(char*left,char* right){
 int lenLeft,lenRight;
 lenLeft = strlen(left);
 lenRight = strlen(right);
 int *c = new int[lenRight];
 int start,end,len;
 end = len = 0;
 for(int i = 0; i < lenLeft; i++){
  for(int j = lenRight-1; j >= 0; j--){
   if(left[i] == right[j]){
    if(i == 0 || j == 0)
     c[j] = 1;
    else
     c[j] = c[j-1]+1;
   }
   else
    c[j] = 0;
   if(c[j] > len){
    len = c[j];
    end = j;
   }
  }
 }
 char *p = new char[len+1];
 start = end - len + 1;
 for(i = start; i <= end; i++)
  p[i - start] = right[i];
 p[len] = '/0';
 return p;
}
3. 余弦定理 (向量空间算法)
余弦定理古老而广泛的数学概念,在各个学科及实践中都得到了大量的应用,这里简单的介绍下其在判断两个字符串相似度的应用。
在余弦定理中基本的公式为:

假如字符串s1与s2,比较两个字符串的相似度,sim(s1,s2),假设s1,s2中含有n个不同的字符,其分别为c1,c2,...cn,判断字符串的相似度转换为两个字符串对应的向量v1,v2之间夹角大小的判断,余弦值越大其向量之间的夹角越小,s1与S2的相似度越大。
向量空间算法的介绍:
在向量空间模型中,文本泛指各种机器可读的记录。用D(Document)表示,特征项(Term,用t表示)是指出现在文档D中且能够代表该文档内容的基本语言单位,主要是由词或者短语构成,文本可以用特征项集表示为D(T1,T2,…,Tn),其中Tk是特征项,1<=k<=N。例如一篇文档中有a、b、c、d四个特征项,那么这篇文档就可以表示为D(a,b,c,d)。对含有n个特征项的文本而言,通常会给每个特征项赋予一定的权重表示其重要程度。即D=D(T1,W1;T2,W2;…,Tn,Wn),简记为D=D(W1,W2,…,Wn),我们把它叫做文本D的向量表示。其中Wk是Tk的权重,1<=k<=N。在上面那个例子中,假设a、b、c、d的权重分别为30,20,20,10,那么该文本的向量表示为D(30,20,20,10)。在向量空间模型中,两个文本D1和D2之间的内容相关度Sim(D1,D2)常用向量之间夹角的余弦值表示,公式为:  


其中,W1k、W2k分别表示文本D1和D2第K个特征项的权值,1<=k<=N。我们可以利用类似的方法来计算两个字符串的相关度。    
这个算法网上没找到,虽然我写过,但是没什么通用性,就不贴出来。很简单的,有兴趣的可以自己写一个


————————————————————————————————————————————————————————————————————






一直不理解,为什么要计算两个字符串的相似度呢。什么叫做两个字符串的相似度。经常看别人的博客,碰到比较牛的人,然后就翻了翻,终于找到了比较全面的答案和为什么要计算字符串相似度的解释。因为搜索引擎要把通过爬虫抓取的页面给记录下来,那么除了通过记录url是否被访问过之外,还可以这样,比较两个页面的相似度,因为不同的url中可能记录着相同的内容,这样,就不必再次记录到搜索引擎的存储空间中去了。还有,大家毕业的时候都写过论文吧,我们论文的查重系统相信也会采用计算两个字符串相似度这个概念。

以下叙述摘自编程之美一书:

许多程序会大量使用字符串。对于不同的字符串,我们希望能够有办法判断其相似程序。我们定义一套操作方法来把两个不相同的字符串变得相同,具体的操作方法为:
1.修改一个字符(如把“a”替换为“b”);  
2.增加一个字符(如把“abdd”变为“aebdd”);
3.删除一个字符(如把“travelling”变为“traveling”);
比如,对于“abcdefg”和“abcdef”两个字符串来说,我们认为可以通过增加/减少一个“g”的方式来达到目的。上面的两种方案,都仅需要一 次 。把这个操作所需要的次数定义为两个字符串的距离,而相似度等于“距离+1”的倒数。也就是说,“abcdefg”和“abcdef”的距离为1,相似度 为1/2=0.5。
给定任意两个字符串,你是否能写出一个算法来计算它们的相似度呢?
原文的分析与解法  
   不难看出,两个字符串的距离肯定不超过它们的长度之和(我们可以通过删除操作把两个串都转化为空串)。虽然这个结论对结果没有帮助,但至少可以知道,任意两个字符串的距离都是有限的。我们还是就住集中考虑如何才能把这个问题转化成规模较小的同样的子问题。如果有两个串A=xabcdae和B=xfdfa,它们的第一个字符是 相同的,只要计算A[2,...,7]=abcdae和B[2,...,5]=fdfa的距离就可以了。但是如果两个串的第一个字符不相同,那么可以进行 如下的操作(lenA和lenB分别是A串和B串的长度)。

1.删除A串的第一个字符,然后计算A[2,...,lenA]和B[1,...,lenB]的距离。
2.删除B串的第一个字符,然后计算A[1,...,lenA]和B[2,...,lenB]的距离。
3.修改A串的第一个字符为B串的第一个字符,然后计算A[2,...,lenA]和B[2,...,lenB]的距离。
4.修改B串的第一个字符为A串的第一个字符,然后计算A[2,...,lenA]和B[2,...,lenB]的距离。
5.增加B串的第一个字符到A串的第一个字符之前,然后计算A[1,...,lenA]和B[2,...,lenB]的距离。
6.增加A串的第一个字符到B串的第一个字符之前,然后计算A[2,...,lenA]和B[1,...,lenB]的距离。
在这个题目中,我们并不在乎两个字符串变得相等之后的字符串是怎样的。所以,可以将上面的6个操作合并为:
1.一步操作之后,再将A[2,...,lenA]和B[1,...,lenB]变成相字符串。
2.一步操作之后,再将A[2,...,lenA]和B[2,...,lenB]变成相字符串。
3.一步操作之后,再将A[1,...,lenA]和B[2,...,lenB]变成相字符串。

通过以上1和6,2和5,3和4的结合操作,最后两个字符串每个对应的字符会相同,但是这三种操作产生的最终的两个字符串是不一样的。我们不知道通过上述的三种结合那种使用的操作次数是最少的。所以我们要比较操作次数来求得最小值。

下面这幅图是摘自编程之美:从中我们可以看出一些信息。

                                             

可以看到,在计算的过程中,有索引越界的情况,抓住这个特点,就可以尽早的结束程序,同时还有重复计算的情况,比如(strA, 2, 2, strB, 2, 2).为了减少计算的次数,可以采用临时数组存储中间的结果。下面给出程序:

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4. int min(int a, int b, int c) {  
  5.     if(a < b) {  
  6.         if(a < c)  
  7.             return a;  
  8.         else   
  9.             return c;  
  10.     } else {  
  11.         if(b < c)  
  12.             return b;  
  13.         else   
  14.             return c;  
  15.     }  
  16. }  
  17. int compute_distance(char *strA, int pABegin, int pAEnd, char *strB, int pBBegin, int pBEnd, int **temp) {  
  18.     int a, b, c;  
  19.     if(pABegin > pAEnd) {  
  20.         if(pBBegin > pBEnd) {  
  21.             return 0;  
  22.         } else {  
  23.             return pBEnd - pBBegin + 1;  
  24.         }  
  25.     }  
  26.   
  27.     if(pBBegin > pBEnd) {  
  28.         if(pABegin > pAEnd) {  
  29.             return 0;  
  30.         } else {  
  31.             return pAEnd - pABegin + 1;  
  32.         }  
  33.     }  
  34.   
  35.     if(strA[pABegin] == strB[pBBegin]) {  
  36.         if(temp[pABegin + 1][pBBegin + 1] != 0) {  
  37.             a = temp[pABegin + 1][pBBegin + 1];  
  38.         } else {  
  39.             a = compute_distance(strA, pABegin + 1, pAEnd, strB, pBBegin + 1, pBEnd, temp);  
  40.         }  
  41.         return a;  
  42.     } else {  
  43.         if(temp[pABegin + 1][pBBegin + 1] != 0) {  
  44.             a = temp[pABegin + 1][pBBegin + 1];  
  45.         } else {  
  46.             a = compute_distance(strA, pABegin + 1, pAEnd, strB, pBBegin + 1, pBEnd, temp);  
  47.             temp[pABegin + 1][pBBegin + 1] = a;  
  48.         }  
  49.   
  50.         if(temp[pABegin + 1][pBBegin] != 0) {  
  51.             b = temp[pABegin + 1][pBBegin];  
  52.         } else {  
  53.             b = compute_distance(strA, pABegin + 1, pAEnd, strB, pBBegin, pBEnd, temp);  
  54.             temp[pABegin + 1][pBBegin] = b;  
  55.         }  
  56.   
  57.         if(temp[pABegin][pBBegin + 1] != 0) {  
  58.             c = temp[pABegin][pBBegin + 1];  
  59.         } else {  
  60.             c = compute_distance(strA, pABegin, pAEnd, strB, pBBegin + 1, pBEnd, temp);  
  61.             temp[pABegin][pBBegin + 1] = c;  
  62.         }  
  63.   
  64.         return min(a, b, c) + 1;  
  65.     }  
  66.   
  67. }  
  68.   
  69. void main() {  
  70.     char a[] = "efsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdefgefsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdefg";  
  71.     char b[] = "efsdfdabcdefgaabcdefgaaefsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdabcdefggaabcdefgasfabcdefg";  
  72.     int len_a = strlen(a);  
  73.     int len_b = strlen(b);  
  74.   
  75.     int **temp = (int**)malloc(sizeof(int*) * (len_a + 1));  
  76.     for(int i = 0; i < len_a + 1; i++) {  
  77.         temp[i] = (int*)malloc(sizeof(int) * (len_b + 1));  
  78.         memset(temp[i], 0, sizeof(int) * (len_b + 1));  
  79.     }  
  80.     memset(temp, 0, sizeof(temp));  
  81.     int distance = compute_distance(a, 0, len_a - 1, b, 0, len_b - 1, temp);  
  82.     printf("%d\n", distance);  
  83. }  

之所以在return min(a, b, c) + 1,进行加1的操作,是为了表示在当前两个对应位置的字符不相等的时候,我们采取了一次操作,不管是上述6种情况中的哪一种。而当两个字符相等的时候,就不需要加1,因为没有进行操作。这种方法是从前向后的操作,还有一种就是采用动态规划的形式。如果想理解动态规划的这种方式,建议先学习求两个字符串的最常公共子序列。所谓最常公共子序列就是给定两个字符串s1,s2,找出一个最常的字符串s3,s3中的每个字符同时在s1,s2中出现,但是不必连续。下面给出应用动态规划解决字符串相似度的解释和程序源码.

设Ai为字符串A(a1a2a3 … am)的前i个字符(即为a1,a2,a3 … ai)
设Bj为字符串B(b1b2b3 … bn)的前j个字符(即为b1,b2,b3 … bj)
设 L(i,j)为使两个字符串和Ai和Bj相等的最小操作次数。
当ai==bj时 显然 L(i,j) = L(i-1,j-1)
当ai!=bj时 
 若将它们修改为相等,则对两个字符串至少还要操作L(i-1,j-1)次
 若删除ai或在bj后添加ai,则对两个字符串至少还要操作L(i-1,j)次
 若删除bj或在ai后添加bj,则对两个字符串至少还要操作L(i,j-1)次
 此时L(i,j) = min( L(i-1,j-1), L(i-1,j), L(i,j-1) ) + 1 
显然,L(i,0)=i,L(0,j)=j, 再利用上述的递推公式,可以直接计算出L(i,j)值。L(i,0)代表Ai和B0,如果想把一个字符串和一个空字符串变的相同,那么之后删除非空串中的字符或者把空串变成和非空串相同的字符串,那么所需要操作的次数为i次。

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4. int min(int a, int b, int c) {  
  5.     if(a < b) {  
  6.         if(a < c)  
  7.             return a;  
  8.         else   
  9.             return c;  
  10.     } else {  
  11.         if(b < c)  
  12.             return b;  
  13.         else   
  14.             return c;  
  15.     }  
  16. }  
  17. int compute_distance(char *strA, int len_a, char *strB, int len_b, int **temp) {  
  18.     int i, j;  
  19.   
  20.     for(i = 1; i <= len_a; i++) {  
  21.         temp[i][0] = i;  
  22.     }  
  23.   
  24.     for(j = 1; j <= len_b; j++) {  
  25.         temp[0][j] = j;  
  26.     }  
  27.   
  28.     temp[0][0] = 0;  
  29.   
  30.     for(i = 1; i <= len_a; i++) {  
  31.         for(j = 1; j <= len_b; j++) {  
  32.             if(strA[i -1] == strB[j - 1]) {  
  33.                 temp[i][j] = temp[i - 1][j - 1];  
  34.             } else {  
  35.                 temp[i][j] = min(temp[i - 1][j], temp[i][j - 1], temp[i - 1][j - 1]) + 1;  
  36.             }  
  37.         }  
  38.     }  
  39.     return temp[len_a][len_b];  
  40. }  
  41.   
  42. void main() {  
  43.     char a[] = "efsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdefgefsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdefg";  
  44.     char b[] = "efsdfdabcdefgaabcdefgaaefsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdabcdefggaabcdefgasfabcdefg";  
  45.     int len_a = strlen(a);  
  46.     int len_b = strlen(b);  
  47.   
  48.     int **temp = (int**)malloc(sizeof(int*) * (len_a + 1));  
  49.     for(int i = 0; i < len_a + 1; i++) {  
  50.         temp[i] = (int*)malloc(sizeof(int) * (len_b + 1));  
  51.         memset(temp[i], 0, sizeof(int) * (len_b + 1));  
  52.     }  
  53.     int distance = compute_distance(a, len_a, b, len_b, temp);  
  54.     printf("%d\n", distance);  
  55. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以通过编写Levenshtein Distance算法来计算字符串相似。以下是一个基于动态规划的实现示例代码: ```java public class LevenshteinDistance { public static int calculate(String s1, String s2) { int[][] distance = new int[s1.length() + 1][s2.length() + 1]; for (int i = 0; i <= s1.length(); i++) { distance[i][0] = i; } for (int j = 1; j <= s2.length(); j++) { distance[0][j] = j; } for (int i = 1; i <= s1.length(); i++) { for (int j = 1; j <= s2.length(); j++) { int cost = s1.charAt(i - 1) == s2.charAt(j - 1) ? 0 : 1; distance[i][j] = Math.min(Math.min(distance[i - 1][j] + 1, distance[i][j - 1] + 1), distance[i - 1][j - 1] + cost); } } return distance[s1.length()][s2.length()]; } } ``` 在上述代码中,我们首先定义一个二维数组distance,用于存储字符串s1和s2之间的编辑距离。然后,我们初始化distance数组的第一行和第一列。接着,我们通过两个嵌套的循环遍历distance数组,计算编辑距离。最后,我们返回distance数组的最后一个元素,即s1和s2之间的编辑距离。 可以通过以下方式调用LevenshteinDistance类中的calculate方法来计算两个字符串之间的编辑距离: ```java String s1 = "kitten"; String s2 = "sitting"; int distance = LevenshteinDistance.calculate(s1, s2); System.out.println("编辑距离为:" + distance); ``` 运行结果为: ``` 编辑距离为:3 ``` 这表明,将字符串"kitten"转变成字符串"sitting"所需的最少操作次数为3。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值