gpt4 book ai didi

vb.net - 查找字符串中的重复序列

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:01:34 24 4
gpt4 key购买 nike

我想在 VB.Net 中的字符串中找到重复序列,例如:

Dim test as String = "EDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGB"

我希望程序检测重复序列,以防 EDCRFVTGB 并计算它重复了多少次。我的问题是找到字符串中的重复序列,我搜索了几种方法但没有找到解决方案,我尝试了快速排序算法、重复算法,但其中有几个不适用于字符串。

虽然我想创建子字符串并检查它们在字符串中的存在,但我不知道如何获取子字符串,因为字符串上没有模式,也有可能字符串中没有重复序列.

最佳答案

首先检查目标字符串的一半是否重复两次。如果不是,检查字符串的三分之一是否重复三次。如果不是,检查字符串的四分之一是否重复四次。这样做直到找到匹配的序列。跳过商不是整数的任何除数,以使其表现更好。这段代码应该可以解决这个问题并填补这个描述未能阐明的任何空白:

Public Function DetermineSequence(ByVal strTarget As String) As String

Dim strSequence As String = String.Empty

Dim intLengthOfTarget As Integer = strTarget.Length

'Check for a valid Target string.
If intLengthOfTarget > 2 Then

'Try 1/2 of Target, 1/3 of Target, 1/4 of Target, etc until sequence is found.
Dim intCursor As Integer = 2

Do Until strSequence.Length > 0 OrElse intCursor = intLengthOfTarget

'Don't even test the string if its length is not a divisor (to an Integer) of the length of the target String.
If IsDividendDivisibleByDivisor(strTarget.Length, intCursor) Then

'Get the possible sequence.
Dim strPossibleSequence As String = strTarget.Substring(0, (intLengthOfTarget / intCursor))

'See if this possible sequence actually is the repeated String.
If IsPossibleSequenceRepeatedThroughoutTarget(strPossibleSequence, strTarget) Then

'The repeated sequence has been found.
strSequence = strPossibleSequence

End If

End If

intCursor += 1

Loop

End If

Return strSequence

End Function

Private Function IsDividendDivisibleByDivisor(ByVal intDividend As Integer, ByVal intDivisor As Integer) As Boolean

Dim bolDividendIsDivisbleByDivisor As Boolean = False

Dim intOutput As Integer

If Integer.TryParse((intDividend / intDivisor), intOutput) Then

bolDividendIsDivisbleByDivisor = True

End If

Return bolDividendIsDivisbleByDivisor

End Function

Private Function IsPossibleSequenceRepeatedThroughoutTarget(ByVal strPossibleSequence As String, ByVal strTarget As String) As Boolean

Dim bolPossibleSequenceIsRepeatedThroughoutTarget As Boolean = False

Dim intLengthOfTarget As Integer = strTarget.Length
Dim intLengthOfPossibleSequence As Integer = strPossibleSequence.Length

Dim bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated As Boolean = False

Dim intCursor As Integer = 1

Do Until (intCursor * intLengthOfPossibleSequence) = strTarget.Length OrElse bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated

If strTarget.Substring((intCursor * intLengthOfPossibleSequence), intLengthOfPossibleSequence) <> strPossibleSequence Then

bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated = True

End If

intCursor += 1

Loop

If Not bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated Then

bolPossibleSequenceIsRepeatedThroughoutTarget = True

End If

Return bolPossibleSequenceIsRepeatedThroughoutTarget

End Function

关于vb.net - 查找字符串中的重复序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com