最长公共子串问题

最长公共子串问题( longest common substring problem)也就是找到两个或以上的字符串的最长公共子串,该字串在位置上相邻

例如ABAB,BABA,ABBA,最长公共子串为AB。

 

我们可以把该问题定义如下:

给出两个字符串S和T,S的长度为m,T的长度为n,找出S与T的最长公共子串。

假设S = “ABAB”,T=“BABA”,我们可以构造一个如下的矩阵:

 

 

 ABAB
B0101
A1020
B0203
A1030

 

对于数组array[m][n]。

  • if( S[i] == T[j] )

                   if((i==0)||(j==0)) 

                        {

                          array[i][i]=1;

                         }

                      else{ 

                       array[i+1][j+1] = array[i][j] + 1;

                        }

  • if( S[i] != T[j] ) array[i+1][j+1] = 0
 
以下给出源码:(说明代码中存储方式array[m][n])
复制代码
 1 #include <string.h>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 
 5 const char * LongestCommonSubstring(const char * strA, const char * strB)
 6 {
 7     char * LCS = NULL;
 8     const size_t LengthA = strlen(strA);
 9     const size_t LengthB = strlen(strB);
10     size_t LCSLength = 0;
11     unsigned int PositionX = 0;
12     unsigned int PositionY = 0;
13     
14     int i, j;
15     int Matrix[LengthA ][LengthB];
16     
17     for(i = 0; i < LengthA ; ++i)
18     {
19         for(j = 0; j < LengthB ; ++j)
20         {
21             Matrix[i][j] = 0;
22         }
23     }    
24     
25     for(i = 0; i < LengthA; ++i)
26     {
27         for(j = 0; j < LengthB; ++j)
28         {
29             if(strA[i] == strB[j])
30             {
31                 if((i == 0)||(j == 0))
32                     Matrix[i][j] = 1;
33                 else                
34                     Matrix[i][j] = Matrix[i - 1][j - 1] + 1;
35             }
36             if(Matrix[i][j] > LCSLength)
37             {
38                 LCSLength = Matrix[i][j];
39                 PositionX = i;
40                 PositionY = j;
41             }
42         }
43     }        
44     
45     
46     LCS = (char *)malloc(LCSLength + 1);
47     int index = LCSLength - 1;
48     while(index >= 0)
49     {
50         LCS[index] = strA[PositionX];
51         --index;
52         --PositionX;
53     }
54     LCS[LCSLength] = '\0';    
55     
56     return LCS;
57 }
58 int main(int argc, char **argv) 
59 {
60     const char * strA = "abab";
61     const char * strB = "baba";
62     const char * LCS = LongestCommonSubstring(strA, strB);
63     printf("Longest Common Substring is %s\n", LCS);
64     return 0;
65 }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值