【发布时间】: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。
【问题讨论】:
标签: arrays vb.net string pattern-matching