Java LCS Algorithm

Source: Internet
Author: User
Import Java. util. arraylist; import Java. util. arrays; import Java. util. collection; import Java. util. hashset; import Java. util. list;/*** LCS (longest common subsequence) find the longest Public String series ** @ author zzf **/public class LCS {private final static string [] Empty = new string [0]; /*** @ return the longest Public String (may have multiple identical lengths) */public static string [] LCS (string S1, string S2, Boolean norepeat) {// exclude common cases if (S1 = NULL | s1.leng Th () = 0 | S2 = NULL | s2.length () = 0) {return empty;} int len1 = s1.length (); int len2 = s2.length (); if (len1 = len2 & s1.equals (S2) {return New String [] {S1};} If (len1> len2 & s1.indexof (S2 )! =-1) {return New String [] {S2};} If (len2> len1 & s2.indexof (S1 )! =-1) {return New String [] {S1};} // construct the comparison matrix char [] chs1 = s1.tochararray (); char [] chs2 = s2.tochararray (); boolean [] [] as = new Boolean [len1] [len2]; for (INT I = 0; I <len1; I ++) {char == chs1 [I]; for (Int J = 0; j <len2; j ++) {char CH2 = chs2 [J]; as [I] [J] = shard = CH2 ;}} // collect forward/diagonal int max = 0; collection <string> Buf = norepeat? (New hashset <string> (): (New arraylist <string> (); For (INT I = 0; I <len1; I ++) {for (Int J = 0; j <len2; j ++) {int Len = lcslength (I, j, len1, len2, ); if (LEN> 0 & Len> = max) {max = Len; Buf. add (s1.substring (I, I + Len) ;}}// obtain the longest diagonal (longest matching character) List <string> ret = new arraylist <string> (); for (string S: BUF) {If (S. length () = max) {ret. add (s) ;}} return ret. toarray (empty);}/*** recursive calculation length (diagonal) */Private Static int lcslength (int I, Int J, int len1, int len2, boolean [] [] as) {if (as [I] [J]) {int c = 1; I ++; j ++; if (I <len1 & J <len2) {C + = lcslength (I, j, len1, len2, as) ;}return C ;}return 0 ;} public static void main (string [] ARGs) {string S1 = "12345"; string S2 = "12456"; // output [] system. out. println (arrays. aslist (LCS (S1, S2, true) ;}{ string S1 = "123456"; string S2 = "1205678"; // output [] system. out. println (arrays. aslist (LCS (S1, S2, true )));}}}

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.