【问题标题】:Matching the closest supported Resolution in an array, vb.net strings匹配数组中最接近的受支持分辨率,vb.net 字符串
【发布时间】:2014-04-18 04:22:29
【问题描述】:

我有多个支持不同分辨率的设备。我想将它们全部分配给相同的分辨率,或者它支持的最接近的分辨率。

所以,例如。如果我想要的分辨率为“1280x720”并且设备支持 1280x960 和 160x120 的视频分辨率,我希望我的代码选择 1280x960。

问题是我当前的设置尝试使用 http://en.wikipedia.org/wiki/Levenshtein_distance 选择字符串的最近“距离”

   'match reswidth to closest support 
    For i As Integer = 0 To ResArray.Count - 1
        'get string distance/difference amount
        LevDist(i) = LevenshteinDistance(ResArray(i), templateRes)
    Next

    Dim min = LevDist.Min()

    Dim x = ResArray(Array.IndexOf(LevDist, min))

    Return x

还有我从受信任站点上得到的 Lev 函数:

 Public Function LevenshteinDistance(ByVal s As String,
                ByVal t As String) As Integer
    Dim n As Integer = s.Length
    Dim m As Integer = t.Length
    Dim d(n + 1, m + 1) As Integer

    If n = 0 Then
        Return m
    End If

    If m = 0 Then
        Return n
    End If

    Dim i As Integer
    Dim j As Integer

    For i = 0 To n
        d(i, 0) = i
    Next

    For j = 0 To m
        d(0, j) = j
    Next

    For i = 1 To n
        For j = 1 To m

            Dim cost As Integer
            If t(j - 1) = s(i - 1) Then
                cost = 0
            Else
                cost = 1
            End If

            d(i, j) = Math.Min(Math.Min(d(i - 1, j) + 1, d(i, j - 1) + 1),
                       d(i - 1, j - 1) + cost)
        Next
    Next

    Return d(n, m)
End Function

我的问题是,它只是告诉你字符串中最接近的函数。如果我在其中有相同的字符串,但它们都在不同的地方怎么办?它不会选择最接近的分辨率,而是选择最接近的字符串。我认为这不是最好的方法。

关于如何找到数组中最近的一个有什么建议吗?

我看到另一个堆栈流链接与一个用于 java 的链接,但我真的不明白。我也不能把它翻译成 vb.net。

(Stack flow php)

【问题讨论】:

    标签: arrays vb.net string pattern-matching


    【解决方案1】:

    可能有更聪明的方法可以做到这一点,但为什么不尝试一些简单的方法,例如将所需分辨率的乘积与可用分辨率的乘积进行比较,然后选择最接近所需分辨率的方法:

    期望:

    1280 x 720 = 921600
    

    可用:

    1280 x 960 = 1228800
    160 x 120 = 19200
    

    【讨论】:

    • 哦,我没想到产品。这是很好的想法。我还尝试了与分辨率宽度的最小距离,但这包括两者。
    • 我认为它应该对你有用,并且应该比 Leventshtein 距离简单得多。
    猜你喜欢
    • 2023-03-29
    • 2012-11-28
    • 2017-03-29
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多