vb.net – 在字符串中查找重复序列

vb.net – 在字符串中查找重复序列,第1张

概述我想在VB.Net中的字符串中找到重复序列,如: 昏暗测试为String =“EDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGB” 我希望程序检测重复序列,以防EDCRFVTGB并计算它重复的次数.我的问题是找到字符串中的重复序列,我搜索了几种方法来做它但我没有得到解决方案,我尝试了快速排序算法,重复算法,但其中一些不能用 我想在VB.Net中的字符串中找到重复序列,如:

昏暗测试为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 strSequenceEnd FunctionPrivate 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 boldivIDendisdivisbleBydivisorEnd FunctionPrivate 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 bolPossibleSequenceIsRepeatedThroughoutTargetEnd Function
总结

以上是内存溢出为你收集整理的vb.net – 在字符串中查找重复序列全部内容,希望文章能够帮你解决vb.net – 在字符串中查找重复序列所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1209727.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存