1

この機能について助けが必要です。2 つの文字列の間で最も長い共通文字列を見つけようとしています。私が現在使用している関数は次のとおりです。

Public Shared Function LCS(str1 As Char(), str2 As Char())
    Dim l As Integer(,) = New Integer(str1.Length - 1, str2.Length - 1) {}
    Dim lcs__1 As Integer = -1
    Dim substr As String = String.Empty
    Dim [end] As Integer = -1

    For i As Integer = 0 To str1.Length - 1
        For j As Integer = 0 To str2.Length - 1
            If str1(i) = str2(j) Then
                If i = 0 OrElse j = 0 Then
                    l(i, j) = 1
                Else
                    l(i, j) = l(i - 1, j - 1) + 1
                End If
                If l(i, j) > lcs__1 Then
                    lcs__1 = l(i, j)
                    [end] = i

                End If
            Else
                l(i, j) = 0
            End If
        Next
    Next

    For i As Integer = [end] - lcs__1 + 1 To [end]
        substr += str1(i)
    Next

    Return substr
End Function

これは、約 600 語程度までの文字列でうまく機能します。それよりも単語数が多い文字列を比較しようとすると、system.outofmemoryexceptionがスローされ始めます。明らかに、これはメモリにかなりの打撃を与えています。この機能を微調整する方法はありますか、それともより合理化された別の方法がありますか?

4

0 に答える 0