且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从两个字符串中找到匹配的子字符串

更新时间:2023-02-22 12:30:58

它称为最长公共子字符串,并且这里有一个C#实现: ^ ]
It''s called the Longest Common Substring, and there is a C# implementation here: Longest Common Substring[^]


尝试
算法实现/字符串/最长的通用子字符串 [
Try
Algorithm Implementation/Strings/Longest common substring[^]
From reference link :-
public int LongestCommonSubstring(string str1, string str2)
{
        if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2))
                return 0;

        int[,] num = new int[str1.Length, str2.Length];
        int maxlen = 0;

        for (int i = 0; i < str1.Length; i++)
        {
                for (int j = 0; j < str2.Length; j++)
                {
                        if (str1[i] != str2[j])
                                num[i, j] = 0;
                        else
                        {
                                if ((i == 0) || (j == 0))
                                        num[i, j] = 1;
                                else
                                        num[i, j] = 1 + num[i - 1, j - 1];

                                if (num[i, j] > maxlen)
                                {
                                        maxlen = num[i, j];
                                }
                        }
                }
        }
        return maxlen;


请参阅此
最长连续且最大连续的子字符串 [ http://www.alexandre-gomes.com/?p=177 [
Refer this
The Longest Common Substring with Maximal Consecutive[^]
http://www.alexandre-gomes.com/?p=177[^]