The 3rd Chapter structure Method--Calculate the similarity of the string

Source: Internet
Author: User

Description of the similarity problem of the computed string

Analysis and Solution

The specific code is as follows:

1  PackageChapter3jiegouzhifa. stringsimilarity;2 /**3 * Calculates the similarity of a string4 * "Solution One"5  * @authorDELL6  *7  */8  Public classstringsimilarity {9      Public Static intCalculatestringdistance (String str1,intB1,intE1, String str2,intB2,intE2) {Ten         if(b1>E1) { One             if(b2>E2) A                 return0; -             Else -                 returnE2-b2+1; the         } -         if(b2>E2) { -             if(b1>E1) -                 return0; +             Else -                 returnE1-b1+1; +         } A         if(Str1.charat (B1) = =Str2.charat (B2)) { at             returnCalculatestringdistance (str1,b1+1,e1,str2,b2+1, E2); -}Else{ -             intT1 = calculatestringdistance (str1,b1+1, e1,str2,b2,e2); -             intT2 = calculatestringdistance (str1,b1,e1,str2,b2+1, E2); -             intT3 = calculatestringdistance (str1,b1+1,e1,str2,b2+1, E2); -             returnMath.min (math.min (t1, T2), T3) +1; in         } -     } to      Public Static voidMain (string[] args) { +String S1 = "Xabcdae"; -String s2 = "Xfdfa"; theThe distance between System.out.println (s1+ "and" +s2+ "is:" +calculatestringdistance (S1,0,s1.length () -1,s2,0,s2.length ()-1)); *  $     }Panax Notoginseng  -}

The results of the program run as follows:

The distance between Xabcdae and Xfdfa is: 5
The following code listing 3-6 is incorrect, and the last T1,T2,T3 calculation error.

The improved program is as follows:

1  PackageChapter3jiegouzhifa. stringsimilarity;2 /**3 * Calculates the similarity of a string4 * "solution two" improved algorithm5 * Use space to change time6  * @authorDELL7  *8  */9  Public classStringSimilarity2 {Ten      Public Static intCalculatestringdistance (String str1,intB1,intE1, String str2,intB2,intE2) { One         intS[][][][] =New int[Str1.length () +1] [Str1.length ()] [Str2.length () +1][str2.length ()]; A         if(b1>E1) { -             if(b2>E2) -                 return0; the             Else -                 returnE2-b2+1; -         } -         if(b2>E2) { +             if(b1>E1) -                 return0; +             Else A                 returnE1-b1+1; at         } -         if(s[b1][e1][b2][e2]!=0) -             returnS[b1][e1][b2][e2]; -         Else{ -             if(Str1.charat (B1) = =Str2.charat (B2)) {             -S[b1][e1][b2][e2]=calculatestringdistance (str1,b1+1,e1,str2,b2+1, E2); in                 returnS[b1][e1][b2][e2]; -}Else{ to                 intT1,t2,t3; +                 if(s[b1+1][e1][b2][e2]!=0) -T1 = s[b1+1][e1][b2][e2]; the                 Else{ *S[B1+1][E1][B2][E2] = calculatestringdistance (str1,b1+1, e1,str2,b2,e2); $T1 = s[b1+1][e1][b2][e2];Panax Notoginseng                 } -                 if(s[b1][e1][b2+1][e2]!=0) theT2 = s[b1][e1][b2+1][e2]; +                 Else{ AS[B1][E1][B2+1][E2] = calculatestringdistance (str1,b1,e1,str2,b2+1, E2); theT2 = s[b1][e1][b2+1][e2]; +                 } -                 if(s[b1+1][e1][b2+1][e2]!=0) $T3 = s[b1+1][e1][b2+1][e2]; $                 Else{ -S[B1+1][E1][B2+1][E2] = calculatestringdistance (str1,b1+1,e1,str2,b2+1, E2); -T3 = s[b1+1][e1][b2+1][e2]; the                 } -S[B1][E1][B2][E2] = math.min (math.min (t1, T2), T3) +1;Wuyi                 returnS[b1][e1][b2][e2]; the             } -         }     Wu     } -      Public Static voidMain (string[] args) { AboutString S1 = "Xabcdae"; $String s2 = "Xfdfa"; -The distance between System.out.println (s1+ "and" +s2+ "is:" +calculatestringdistance (S1,0,s1.length () -1,s2,0,s2.length ()-1)); -  -     } A  +}

The results of the program run as follows:

The distance between Xabcdae and Xfdfa is: 5

The 3rd Chapter structure Method--Calculate the similarity of the string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.