Results 1 to 8 of 8

Thread: Strings & math! help me out guys

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    Something like this?

    VB Code:
    1. Sub SubStrings(strFirst As String, strSecond As String)
    2.  
    3. 'counter(i) will contain the no. of times a substring
    4. 'length i of strSecond appears in strFirst
    5.  
    6. Dim tmparr() As String
    7. Dim tmpStr As String
    8. Dim Counter() As Long
    9. Dim i As Long, j As Long
    10.  
    11. ReDim Counter(Len(strSecond) )
    12.  
    13. For i = 1 To Len(strSecond)
    14.     For j = 1 To Len(strSecond) - i + 1
    15.         tmpStr = Mid$(strSecond, j, i)
    16.         tmparr() = Split(strFirst, tmpStr, , vbBinaryCompare)
    17.         Counter(i) = UBound(tmparr()) + Counter(i)
    18.     Next j
    19. Next i
    20.  
    21. End Sub

  2. #2
    DerFarm
    Guest
    Ok, I'll take a shot.

    I have a function DelimitedString_NumberofStrings that will
    retrieve the number of pieces of strings that are delimited by a
    constant string:

    xxx = "Thefoxwalksonthefloor"

    DS_NumberOfStrings(xxx,"the") will return 3
    and there are 3 fields:"","foxwalkson",and "floor"

    Assuming that you have this function

    Str1 ="Thefoxwalksonthefloor"
    Str2="Samanthafoxfloorsthewalker"
    i = 1
    hits = 0
    while i < (len(STR2)-2)
    checkit=mid$(STR2,i,3)
    hits = hits+DS_NumberOfStrings(STR1,checkit)
    i = i + 1
    wend

    should do the trick.

    Would you like the function list?

    Good Luck

  3. #3
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    112
    hi guys,

    Thanks for the good work, but....

    i think i didnt explain it correctly Let me try again. Its a mathematical question;

    Str1 ="Thefoxwalksonthefloor"
    Str2="Samanthafoxfloorsthewalker"

    I want to check for how many 'percent' Str1 equals Str2
    (well its not actually 'percents' but i want: The higher the number to bigger the equality.

    I use a method where i delimit the strings in 2 character strings and compare them (wita 2 arrays etc). every time i get a match i add 1 to a counter. (that works fine)

    Now, To get a better result i ALSO want to compare the strings with 3-caracter strings (the same way as above, but then with 3 -char. strings )

    The chance that i hit a match with the 3-char. test is far smaller then with the 2-char test. so a match with the 3-char. match "weighs" heavier then th eother one, so i have to add more then 1 to Counter when i have a 3-char match.

    My question is: how much more "heavy" is a 3-char match?


    Since my native tongue is not english i dont know all the official mathematical language, but i hope u guys understand

    Thanks !!

    Remvs
    Holland

  4. #4
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    112
    Derfarm,

    I'd really appreciate the func.list so i can compare a little.


    i'm a novice at this and probably have messed up my declarations etc. Since i have to compare 2 x 28000 strings (long ones) im also looking for ways to speed the basterd up a little


    thnx in advance
    remvs

  5. #5
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    112
    i dont have the code here now, but i can post it tommorow from work if anyone wants to see it

    remvs

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    VB Code:
    1. Sub SubStrings(strFirst As String, strSecond As String)
    2.  
    3. 'percentcorrect(i) will contain the % of substring of
    4. 'length i of strSecond is succesfully matched with a substring of
    5. 'strFirst
    6.  
    7. Dim tmparr() As String
    8. Dim tmpStr As String
    9. Dim Counter As Long
    10. Dim i As Long, j As Long
    11. Dim percentcorrect() As Long
    12.  
    13. ReDim percentcorrect(Len(strSecond))
    14.  
    15. For i = 1 To Len(strSecond)
    16.     For j = 1 To Len(strSecond) - i + 1
    17.         tmpStr = Mid$(strSecond, j, i)
    18.         tmparr() = Split(strFirst, tmpStr, , vbBinaryCompare)
    19.         Counter = UBound(tmparr()) + Counter(i)
    20.     Next j
    21.     percentcorrect(i) = 100 * Counter /(Len(strSecond) - i)
    22. Next i
    23.  
    24. End Sub

    Also, assuming you only use letters in your string, a 3 chr string is 26 / no. of comparisons more likely to occur.

    ie, if you have 2 * 4chr length strings,

    prob. of finding 1 match for a 2chr substring is: 3 * 3 / 26^2
    prob. of finding 1 match for a 3chr substring is: 2 * 2 / 26^3

    General formula for the probability of 1 substring (length x), of String 1 (length y) occuring in String 2 (length z) is:

    (y + 1 - x) * (z + 1 - x) / 26^x

  7. #7
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    112

    Thumbs up

    Chrisf,

    The formula at the bottom was what i needed! with that i can calculate the probability.

    Thnx (all of u guys)

    Remvs

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    You can expand it to find the prob. of n substrings ( length x )
    of string 1 occuring in string 2. It will be a Geometric series,
    First term n= (y + 1 - x) * (z + 1 - x) / 26^x
    Ration = (y + 1 - x)

    Giving: ((y + 1 - x) * (z + 1 - x) / 26^x )* (1 - (y + 1 - x)^ n) / (y - x)

    if I've got the correct ratio.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width