Algorithm to improve the longest character sequence

Source: Internet
Author: User

The longest character sequence

The problem description is set by X (i), Y (i), z (i) to represent a single character, then x={x (1) x (2) ... x (m)},y={y (1) Y (2) ... y (n)},z={z (1) z (2) ...}, we call it a sequence of characters, where M,n and k are character sequence x, respectively. The length of the y,z, the number in parentheses () is called the subscript of the character sequence.
If there is a strict increment and the length is greater than 0 of the subscript sequence {i1, I2... Ik}, so that for all j=1,2,...... K, there is x (iJ) =z (j), then we call z a sequence of characters of X. Also, if z is both a sequence of characters of the x character and y, we call Z the common sequence of characters for X and Y.
In our question today, we want to calculate the common sequence of characters for the maximum length of two given character sequences X and Y, where we only need to output the length value corresponding to the common subsequence of the maximum length.
For example, the character sequence is X=ABCD,Y=ACDE, then their maximum length is 3, and the corresponding public character sequence is ACD. Input format input a line, separated by a space of two string output format output the length of the maximum length of the common character sequence corresponding to the two character sequences sample input AABB Aabb sample output 2 data size and contract input string length of up to 100, case-sensitive.
1 ImportJava.math.BigInteger;2 Importjava.util.Arrays;3 ImportJava.util.Scanner;4 5 6  Public classMain {7     StaticString A;8     StaticString B;9     Static intMax;Ten     StaticStringBuffer S; One     Staticstringbuffer S1; A      Public Static voidMain (string[] args) { -Scanner input =NewScanner (system.in); -A =Input.next (); theb =Input.next (); -s =NewStringBuffer (); -S1 =NewStringBuffer (); -F (0); + System.out.println (max); -          +     } A      Public Static voidFinti) { at         inth; -          for(H=i;h<a.length (); h++){ - S.append (A.charat (h)); -             if(F1 (S.tostring ())) { -                 if(max<s.length ()) { -Max =s.length (); in                 } -             } toF (h+1); +S.deletecharat (S.length ()-1); -         } the     } *      Public Static BooleanF1 (String temp) { $         intj = 0;Panax Notoginseng          for(intI=0;i<b.length (); i++){ -             if(B.charat (i) = =Temp.charat (j)) { theJ + +; +                 if(j==temp.length ()) { A                      Break; the                 } +             } -         } $         if(J==temp.length ())return true; $         Else return false; -     } -}

Algorithm to improve the longest character sequence

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.